Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 478
City Techno Avatar asked Dec 05 '11 16:12

City Techno


2 Answers

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.

like image 98
Oliver Avatar answered Sep 24 '22 02:09

Oliver


Based on Starter's link to Apple docs (my code in Swift 3):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    if let cell = tableView.cellForRow(at: selectedIndexPath) {
        cell.accessoryType = .none
    }
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .checkmark
    }
    selectedIndexPath = indexPath
}

The main point is to keep track of currently selected cell and change cell.accessoryType accordingly. Also don't forget to properly set cell.accessoryType in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) based on selectedIndexPath.

like image 33
algrid Avatar answered Sep 23 '22 02:09

algrid