How to uncheck another click?

I want to create a table that the user can select and deselect using a checkmark:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { ...; cell.selectionStyle = UITableViewCellSelectionStyleNone; } - (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ...; newCell.accessoryType = UITableViewCellAccessoryCheckmark; ...; } 

I tried to remove the checkmark by clicking on the cell with the check again, but this requires 2 clicks, not one.

If I set the default selection style when I click on the 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 a second click.

What causes the problem and how to fix it?

+4
source share
4 answers

You can try the following:

 - (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 checkmark at every touch. If you additionally want to display only one cell at a time, add the following also:

 - (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 need a blue hilight background, just set the cell selection style to UITableViewCellSelectionStyleNone after creating the cell.

+14
source

Based on the Starter 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 thing is to track the current selected cell and change cell.accessoryType accordingly. Also remember to set cell.accessoryType correctly in tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) based on selectedIndexPath.

+1
source

Check this Apple whitepaper

best solution ever

0
source
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (index != NSNotFound) { UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index]; if (cell.accessoryType == UITableViewCellAccessoryNone) cell.accessoryType = UITableViewCellAccessoryCheckmark; else cell.accessoryType = UITableViewCellAccessoryNone; [self.tableName reloadData]; } } 

This helps to switch checkmarks (remove the checkmark when you click on the cell with the check mark again)

0
source

Source: https://habr.com/ru/post/1384732/


All Articles