ios - How to use checkmark for UITableView -
i trying create simple todo list app learning purposes i. want able click on row , add check mark , when clicked again want go away. have looked @ several other examples nothing working. how can achieve this?
class firstviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { @iboutlet weak var tbview: uitableview! override func viewdidload() { super.viewdidload() tbview.reloaddata() } override func viewwillappear(animated: bool) { self.tbview.reloaddata() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int{ return tasks.manager.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell: uitableviewcell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: "default tasks") //assign contents of our var "items" textlabel of each cell cell.textlabel!.text = tasks.manager[indexpath.row].name cell.detailtextlabel!.text = tasks.manager[indexpath.row].time return cell } func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath){ if (editingstyle == uitableviewcelleditingstyle.delete){ tasks.manager.removeatindex(indexpath.row) tbview.reloaddata() } } func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { let cell = tableview.cellforrowatindexpath(indexpath) cell!.accessorytype = .checkmark tableview.reloaddata() } }
to put simply, can use code.
func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { if let cell = tableview.cellforrowatindexpath(indexpath) if (cell!.accessorytype == .checkmark) { cell!.accessorytype = .none } else { cell!.accessorytype = .checkmark } tableview.reloaddata() }
this way, when re-select cell, checkmark adjust accordingly.
however, shouldn't modify cell checkmark way cell re used when scroll. need update data model instead above approach.
so in data model, add new property hold checkmark state , use in cellforrowatindexpath
function.
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell: uitableviewcell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: "default tasks") //assign contents of our var "items" textlabel of each cell cell.textlabel!.text = tasks.manager[indexpath.row].name cell.detailtextlabel!.text = tasks.manager[indexpath.row].time if tasks.manager[indexpath.row].isselected { //assume isselected bool cell!.accessorytype = .checkmark } else { cell!.accessorytype = .none } return cell }
and in didselectrowatindexpath
update model.
func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { let datamodel = tasks.manager[indexpath.row] datamodel.isselected = !datamodel.isselected tableview.reloaddata() }
Comments
Post a Comment