If you select the check box, this code will be executed.
if(self.checkedIndexPath) { UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath]; uncheckCell.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
This code will remove the checkmark that you added during the first selection.
then this code will be executed
if([self.checkedIndexPath isEqual:indexPath]) { self.checkedIndexPath = nil; }
Thus, only when you select the same cell again, a checkmark will appear again
I think a cleaner way would be like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath]; cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:indexPath]; uncheckCell.accessoryType = UITableViewCellAccessoryNone; }
AS you can get the value for self.checkedIndexPath from [tableView indexPathForSelectedRow]; setting self.checkedIndexPath is optional depending on the logic of your code.
source share