ios - Swift - Use different init-method for dequeueReusableCellWithIdentifier -
ios - Swift - Use different init-method for dequeueReusableCellWithIdentifier -
i want utilize own uitableviewcell class, in corresponding uitableviewcontroller write
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier(cellidentitfier, forindexpath: indexpath) mytableviewcell //cellidentifier initialized homecoming cell } i want, however, initialize cell have pass arguments when created. apple docs say, dequeuereusablecellwithidentifier calls (if cell has initialized) initwithstyle:reuseidentifier.
if there cell reused, method calls prepareforreuse.
either way, want pass arguments cell before other methods executed (i. e. @ initialization , in prepareforreuse, respectively).
what appropriate way , there way utilize other initializers defined in class derives uitableviewcell (mytableviewcell)?
if want have custom init method, (a) need create sure not have cell prototype defined (because if do, phone call initializer itself); , (b) cellforrowatindexpath appropriately handles when reused cell not found , hence have instantiate own (with whatever parameters want.
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier(cellidentifier) mytableviewcell? if cell == nil { cell = mytableviewcell(param1: "foo", param2: "bar", reuseidentifier: cellidentifier) // configure new cell here } else { // reconfigure reused cell here } homecoming cell! } personally, though, wouldn't inclined go route. i'd rather take advantage of cell prototypes , standard initialization routines, override properties (or phone call configuration function) necessary, e.g.:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { allow cell = tableview.dequeuereusablecellwithidentifier(cellidentifier, forindexpath: indexpath) mytableviewcell cell.property1 = "foo" cell.property2 = "bar" homecoming cell } this way can enjoy cell prototype, automatic instantiation of cell object class, mapping of iboutlets , ibactions, etc., whatever special configuration need here in cellforrowatindexpath.
ios xcode swift
Comments
Post a Comment