ios - How to add an image when a row is selected and then remove it when it is deselected in UITableView in SWIFT -
i'm having issue making image appear when row selected , making image disappear when row selected. if touch selected row not deselected – ok behaviour want. want selected row deselected when touch row.
i writing in swift.
i using kai engelhardt's solution expand selected row, answered here.
this uiimage should appear/disappear: cellcontent.ringimage.image = uiimage(named: "ring.png")
i'm guessing logic wrong in selectedcellindexpath part below.
this code:
in tvc:
class menuviewcontroller: uiviewcontroller{ var selectedcellindexpath: nsindexpath? let selectedcellheight: cgfloat = 222.0 let unselectedcellheight: cgfloat = 64.0 let menuitems = [ ("1","test 1"), ("2","test 2"), ("3","test 3"), ("4","test 4"), ("5","test 5")] func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { if tableview == menutable { return menuitems.count } else { return 0} } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! menutableviewcell if tableview == menutable { let (my, section) = menuitems[indexpath.row] cell.mylabel.text = cell.sectionlabel.text = section cell.selected = true } } func tableview(tableview: uitableview!, heightforrowatindexpath indexpath: nsindexpath!) -> cgfloat { if let selectedcellindexpath = selectedcellindexpath { if selectedcellindexpath == indexpath { return selectedcellheight } } return unselectedcellheight } func tableview(tableview: uitableview!, didselectrowatindexpath indexpath: nsindexpath!) { var cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! menutableviewcell var cellcontent = tableview.cellforrowatindexpath(indexpath) as! menutableviewcell let celllabel = cellcontent.sectionlabel.text if let selectedcellindexpath = selectedcellindexpath { if selectedcellindexpath != indexpath { self.selectedcellindexpath = indexpath cellcontent.ringimage.image = uiimage(named: "ring.png") } else { self.selectedcellindexpath != indexpath cellcontent.ringimage.hidden = true tableview.deselectrowatindexpath(indexpath, animated: true) // cellcontent.testbutton.removefromsuperview } } else { selectedcellindexpath = indexpath cellcontent.ringimage.hidden = true tableview.deselectrowatindexpath(indexpath, animated: true) } tableview.beginupdates() tableview.endupdates() }
your appreciated!
thanks
mikee
it seems self.selectedcellindexpath != indexpath
not deselect mark effect want. may try use tableview.indexpathsforselectedrows()
selected indexpath, compare indexpath in argument, , complete logic without assigning self.selectedcellindexpath
.
(edited) find need varialbe self.selectedcellindexpath
identify height, try convert counter variable counts selected time of selected row. if odd, selected, while when it's know deselect , reset counter varialbe zero.
func tableview(tableview: uitableview!, heightforrowatindexpath indexpath: nsindexpath!) -> cgfloat { if (indexpath == tableview.indexpathsforselectedrows()[0] && self.counter % 2 == 1) { return selectedcellheight } return unselectedcellheight }
Comments
Post a Comment