iPhone :UITableView CellAccessory Checkmark

Keep track, in an instance variable, of which row is checked. When the user selects a new row, first uncheck the previously checked row, then check the new row and update the instance variable.

Here is more detail. First add a property to keep track of the currently checked row. It's easiest if this is an NSIndexPath.

@interface RootViewController : UITableViewController {
    ...
    NSIndexPath* checkedIndexPath;
    ...
}

...
@property (nonatomic, retain) NSIndexPath* checkedIndexPath;
...

@end

In your cellForRowAtIndexPath add the following:

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

How you code your tableView:didSelectRowAtIndexPath: will depend on the behavior you want. If there always must be a row checked, that is, that if the user clicks on an already checked row use the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}

If you want to allow the user to be able to uncheck the row by clicking on it again use this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                    cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    if([self.checkedIndexPath isEqual:indexPath])
    {
        self.checkedIndexPath = nil;
    }
    else
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.checkedIndexPath = indexPath;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
    int newRow = [indexPath row];
    int oldRow = [lastIndexPath row];
    if (newRow != oldRow)
    {
        newCell = [tableView  cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        lastIndexPath = indexPath;  
    }
}

For reference take a look at this discussion


this is a simple solution that worked for me:

in your .h file declare:

NSIndexPath *checkedCell;

then in your .m file add:

        if (checkedCell == indexPath) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else { 
cell.accessoryType = UITableViewCellAccessoryNone; 
}

to the cellForRowAtIndexPath method and

checkedCell = indexPath;
[tableView reloadData]

to the didSelectRowAtIndexPath method.

Best of luck!


I worked on your problem and got a solution

in .h file

int rowNO;
    NSIndexPath *lastIndexPth;

in . m file

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%d",indexPath.row);
    if (rowNO!=indexPath.row) {
        rowNO=indexPath.row;        
        [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
        [tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone;
        lastIndexPth=indexPath;
    }

I hope this might help you...


its simple

in .h

NSIndexPath   checkedCell;

In .m

cellForRowAtIndexPath


if ([checkedCell isEqual:indexPath]) 

{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;


} else
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
}

in didSelectRowAtIndexPath

checkedCell = indexPath;

[tableView reloadData]