City Techno City Techno - 19 days ago 5
iOS Question

How to remove the check mark on another click?

I want to make a table that user can select and deselect with a check mark:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
...;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...;
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
...;
}


I was trying to remove the check mark when clicking on the check-marked cell again, but it takes 2 clicks to do that instead of one.

If I set selection style to default, when I click on a selected row, it removes the blue highlight; clicking again, it removes the check mark.

I also tried some conditional statements in
didSelectRowAtIndexPath
, but they only respond to second click as well.

What causes the problem and how do I fix it?

Answer

You can try this one:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  NSUInteger index = [[tableView indexPathsForVisibleRows] indexOfObject:indexPath];

  if (index != NSNotFound) {
    UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
    if ([cell accessoryType] == UITableViewCellAccessoryNone) {
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
      [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
  }
}

This should toggle the cell's checkmark on every touch. If you additionally want only one cell to appear selected at a time, also add the following:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger index = [[tableView indexPathsForVisibleRows] indexOfObject:indexPath];
    if (index != NSNotFound) {
        UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
}

If you don't want the blue hilight background, simply set the cell's selection style to UITableViewCellSelectionStyleNone once you create the cell.