Reputation: 1974
I have created a simple tableview where you can check off task that are completed. The code for this specific property looks like this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let mySelectedCell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
mySelectedCell.tintColor = UIColor.blackColor()
}
The problem is that I can not uncheck these cells once they are checked. Does anyone know how this property is added?
Any thoughts on how to proceed would be appreciated.
EDIT: I now know that there is a need for an if-statement, but I do not know how to check if my cell has already been checked.
Upvotes: 2
Views: 2217
Reputation: 1222
If you want to do a simple on an off for aesthetics only...
if (mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark) {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.None
}
else {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
If you want to do it based on a property...
if (!myValue[indexPath.row].value) { // if the BOOL is NO
mySelectedCell.accessoryType = UITableViewCellAccessoryType.None
}
else {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
Upvotes: 4
Reputation: 297
You can't uncheck it because it's assigning the checkmark every time it's called rather than alternating. Use an if statement to see if its checked and switch between accessory types checkmark and none.
Upvotes: 0