How to properly switch the UITableViewCell accesoryType to select / deselect?

I am trying to switch accesoryType when a table cell is selected / deselected ... the behavior should be: tap -> set accessoryType to UITableViewCellAccessoryCheckmark -> tap cell again -> rollback to type UITableViewCellAccessoryNone . The implementation in my controller is this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell setAccessoryType:UITableViewCellAccessoryNone]; } 

... anyway, when the style is configured as UITableViewCellAccessoryCheckmark , I cannot restore it to UITableViewCellAccessoryNone ! I also tried calling:

 [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

but doesn’t remove the check mark ... what should I do?

EDIT: implementation is ok, the problem was in a custom subclass of UITableViewCell ... sorry: P

+4
source share
4 answers

Try it if that's what you want.

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { cell.accessoryType = UITableViewCellAccessoryNone; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; } } 
+13
source

If you want to have only one line as a check mark, use this

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryCheckmark) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark; if (_lastSelectedIndexPath != nil) { UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:_lastSelectedIndexPath]; lastSelectedCell.accessoryType = UITableViewCellAccessoryNone; } _lastSelectedIndexPath = indexPath; } 
+2
source

Clicking on a cell again means selecting a cell, not deselecting it.

You need to have a switch in the method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath to check if cell.accessoryType == UITableViewCellAccessoryCheckmark .

0
source
 - (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath { [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO]; UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath]; if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; // Reflect selection in data model } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { cell.accessoryType = UITableViewCellAccessoryNone; // Reflect deselection in data model } } 
0
source

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


All Articles