How to properly toggle UITableViewCell's accesoryType on cell selection/deselection?

I’m trying to toggle accesoryType when a table cell is selected/deselected… the behavior should be: tap -> set accessoryType to UITableViewCellAccessoryCheckmark -> tap the cell again -> rollback to UITableViewCellAccessoryNone type.
The implementation in my controller is the following:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

…anyway once the style is configured as UITableViewCellAccessoryCheckmark I’m unable to restore it back to UITableViewCellAccessoryNone!
I also tried to call:

  • iPhone: How to increase the height of cell dynamically as per the string
  • Detect hash tags #, mention tags @, in iOS like in Twitter App
  • swipe to delete in a UITableView which is embeded in a UIScrollView
  • Assertion failure when using UISearchDisplayController in UITableViewController
  • Change background of a grouped UITableView
  • Core Data and UITableView Practices/Questions
  • [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    

    but does not remove the checkmark… what should I do?

    EDIT: The implementation is ok, the problem was in the custom UITableViewCell subclass… sorry 😛

    4 Solutions Collect From Internet About “How to properly toggle UITableViewCell's accesoryType on cell selection/deselection?”

    Try this if this is what you want

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
        {   
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
    

    If you want to have only one row as checkmark use this

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryCheckmark) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
        if (_lastSelectedIndexPath != nil)
        {
            UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:_lastSelectedIndexPath];
            lastSelectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
        _lastSelectedIndexPath = indexPath;
    } 
    

    Tapping the cell again equates to selection of the cell and not deselection.

    You need to have a toggle in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method to check if the cell.accessoryType == UITableViewCellAccessoryCheckmark.

    - (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
        [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
        UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            // Reflect selection in data model
        } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            // Reflect deselection in data model
        }
    }