How to track the index of all selected cells in a UITableView

I have a UITableView with approximately 20 rows. I also use the accessory checkmark to identify the selected. The problem I'm facing right now is that the selection got messed up while scrolling (here I select a few lines). Therefore, after scrolling, the selected checkmark disappeared. Can someone help me find a way? in didSelectRowAtIndexPath:

 NSMutableDictionary *rowDict = [tableList objectAtIndex:[indexPath row]]; if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } else { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; checkedIndexPath = indexPath; NSLog(@"the checked psths are :%@",checkedIndexPath); } 

And in cellforrowatIndexpath I use

 if([self.checkedIndexPath isEqual:indexPath]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } 

But this only works for one choice. What to do for multiple choice?

+4
source share
3 answers

You can have an NSMutableArray that can add n NSIndexPath corresponding to the selected cells. When a cell is canceled, you can remove this index path from the array.

+2
source

You must use an array to store validated indexes. If you use one property, only the last selected line will receive the correct accessory.

+1
source

I have used this code many times (with some minor changes as needed)

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; if (thisCell.accessoryType == UITableViewCellAccessoryNone) { thisCell.accessoryType = UITableViewCellAccessoryCheckmark; } else { thisCell.accessoryType = UITableViewCellAccessoryNone; } } - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath { //add your own code to set the cell accesory type. return UITableViewCellAccessoryNone; } 
0
source

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


All Articles