I have made a tableView in which i need to select multiple options. The options are selected but when i scroll the table view the check mark option get disappear and some ot
Check this example
cell.accessoryType = UITableViewCellAccessoryNone;
for (int x = 0; x < selectedIds.count; x++) {
if ([[selectedIds objectAtIndex:x] isEqualToString:[[source objectAtIndex:[indexPath row]] objectForKey:@"id"]])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
In my case just setting accessoryType = UITableViewCellAccessoryCheckmark in cellForRowAtIndexPath didn't work.
The problem occurs when the UITableView reaches its first element and if you try to scroll more, the visible cells go down and their checkmarks gets gone.
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];// tricky part is this line
if ( [selectedCells containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
You need to check the cell is already selected or not in the cellForRowAtIndexPath method. This issue is happening because tableView re-uses the cells.
Please write the below code in your cellForRowAtIndexPath method, it'll solve the issue.
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}