I have 4 sections in my tableview, and when a row is pressed i want a checkmark to be added to that row. With the code below, if i select a row, some of the other rows also get checked, which is not desired. I’m guessing it has something to do with the several sections, but i don’t know how to check for that or fix it. My _allTagsList is a NSMutableArray with 4 dictionaries.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [_allTagsList objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Tags"];
NSString *selectedTag = [array objectAtIndex:indexPath.row];
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
else
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You need to store which indexPaths are selected and then in your cellForRowAtIndexPath function you need t also set the accessoryType based on those saved indexPaths. If you change the accessoryType on a cell and then scroll, as that cell gets recycled the accessoryType will stil remain checked.