Search code examples
iosobjective-cuitableviewdidselectrowatindexpathaccessorytype

Make the accessory type a checkmark for selected cell


I'm trying to make the selected cell (of a custom UITableViewCell) to have a checkmark on it. I tried the following code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategorieCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];

    if (customCell.accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
        customCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        customCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

But when I selected a cell, nothing happened.


Solution

  • Your mistake is that you dequeue a new custom cell. Instead, you have to find out the cell that actually is selected. Change the first line to:

    CategorieCell *customCell = (CategorieCell *)[tableView cellForRowAtIndexPath:indexPath];
    

    This should be it.