Mansi
Mansi

Reputation: 151

Problems with a checkmark in a UITableViewCell

I have implemented this below code.

UITableViewCell *cell = [tableView1 cellForRowAtIndexPath:indexPath];
UITableViewCell *cell2 = [tableView1 cellForRowAtIndexPath:oldIndexPath1];

cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell2.accessoryType = UITableViewCellAccessoryNone;
oldIndexPath1 = indexPath;

But, if I select and then unselect the checkmark, then I cannot select the checkmark anymore.

can you help me?

Upvotes: 2

Views: 4401

Answers (1)

Nick Bedford
Nick Bedford

Reputation: 4435

I think you're confusing the action of changing the check from one to another and the action of toggling one cell only.

Assuming you only want one checkmarked cell, you could do the following:

+(void)toggleCheckmarkedCell:(UITableViewCell *)cell
{
    if (cell.accessoryType == UITableViewCellAccessoryNone)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;
}

// tableView:didSelectRowAtIndexPath:

UITableViewCell *cell = [tableView1 cellForRowAtIndexPath:indexPath];
UITableViewCell *cell2 = [tableView1 cellForRowAtIndexPath:oldIndexPath1];

// Toggle new cell
[MyController toggleCheckmarkedCell:cell];
if (cell != cell2) // only toggle old cell if its a different cell
    [MyController toggleCheckmarkedCell:cell2];
oldIndexPath1 = indexPath;

Upvotes: 6

Related Questions