Talha Ch Talha Ch - 2 months ago 6
Objective-C Question

UITableViewCellAccessoryCheckmark repeating on cells

I've tried every single option available on this website for this problem but none of them seems to be working.
If I scroll down I see the checkmark. If I scroll back up the positioning of my selection is not the same.
So as the problem is the same I wanted to open this question for one more time as a fresh start. Here is how I'm doing it:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

myTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (![selectedDX containsObject:myMutArray[indexPath.row]])
{
[selectedDX addObject:myMutArray[indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

myTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if ([selectedDX containsObject:myMutArray[indexPath.row]]) {
[selectedDX removeObject:myMutArray[indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryNone;
}
}


I've also tried it in in a single didSelectRowAtIndexPath method but the result is same.

Answer

Add condition in cellForRowAtIndexPath

 if([selectedDX containsObject:allCodeLabel[indexPath.row]])
    {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryType.None
    }

It works for me.Hope it helps :)

Comments