ios - Custom TableViewCell Button should toggle active status and icon of a specific row, but it also changes the icon of other unrelated rows -
i need toggle per-object state (active/inactive) in tableview
contains custom objects: want show active objects in tableview
, when state of object changes inactive, disappears list.
the app working (creating new custom objects, displaying them in tableview
, viewing , editing content - including active/inactive status - in detailview...); in case relevant, please consider my project uses core data , use methods populate tableview
.
i need custom design tableview
cells, i've implemented (i have subclass of uitableviewcell
, custom cells displaying desired content) , seems work, it's first time custom cells , must have done wrong...
when tap active-inactive button in cell change custom object status (and make disappear list because has become inactive), i want object (its status changes , object disappears list).
my issue see image of uibutton
(which shows 2 different icons when object active or inactive) of few other rows change inactive icon. button image of rows, except 1 tapped , disappeared, should active one. please consider that, these other rows, it's icon wrong, actual status still active every row haven't tapped: if stop app , run again, on startup icons right, , in tableview
see objects still active.
i'm pretty sure issue related `dequeuereusablecellwithidentifier, don't know fix issue.
here few of methods:
// tableviewcontroller override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) mycustomtableviewcell self.configurecell(cell, atindexpath: indexpath) return cell } func configurecell(cell: mycustomtableviewcell, atindexpath indexpath: nsindexpath) { let aobject = self.fetchedresultscontroller.objectatindexpath(indexpath) customobject cell.customlabel.text = aobject.name cell.detaillabel.text = aobject.anotherproperty //... cell.objecttomarkasinactive = aobject } // mycustomtableviewcell //... iboutlets var objecttomarkasinactive : customobject? @ibaction func markobjectasinactive(sender: anyobject) { activebutton.selected = !activebutton.selected objecttomarkasinactive?.active = !(objecttomarkasinactive?.active bool) savecontext() } override func awakefromnib() { super.awakefromnib() // uiimage vars activebutton.setimage(activeimage, forstate: uicontrolstate.selected) activebutton.setimage(inactiveimage, forstate: uicontrolstate.normal) activebutton.setimage(highlightedimage, forstate: uicontrolstate.highlighted) }
i tried reload tableview
after saving context, icon issue on other rows remains.
clearly, solution set custom property of cell objecttomarkasinactive = aobject in configurecell
of tableviewcontroller isn't right one, since cells reused, have no other ideas accomplish want, since status-changing button of cell has connected ibaction mycustomtableviewcell , not tableviewcontroller.
i'm pretty new programming, , issue caused bad decisions on part, use suggestions, i'm stuck...
i should stop using dequeuereusablecellwithidentifier
? wouldn't it, because potentially have significant amount of objects, if have use normal cells, can have custom design them without deque? "regular" cell instantiation method shows few options normal cells, not custom ones, in enum (uitableviewcellstyle.
).
thanks in advance,
@cdf1982
you not need stop using dequeuereusablecellwithidentifier
. issue cells being reused, if it's image button incorrect, add in check see image needs displayed.
func configurecell(cell: mycustomtableviewcell, atindexpath indexpath: nsindexpath) { let aobject = self.fetchedresultscontroller.objectatindexpath(indexpath) customobject cell.customlabel.text = aobject.name cell.detaillabel.text = aobject.anotherproperty //... if aobject.isactive { // set button active } else { // set button inactive } cell.objecttomarkasinactive = aobject }
Comments
Post a Comment