UITableViewCell prevents deletion

I am looking for a way to prevent one of my cells from being deleted. (The Delete button should not appear next to a cell when the table view is in edit mode.)

How can I do that?

+2


a source to share


2 answers


Inject editStyleForRowAtIndexPath and return UITableViewCellEditingStyleNone for this row:



- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == sss && indexPath.row == rrr)
        return UITableViewCellEditingStyleNone;
    else
        return UITableViewCellEditingStyleDelete;
}

      

+7


a source


The accepted answer works, but this is not the correct way to do it. There are two methods available: editingStyleForRowAtIndexPath

andcanEditRowAtIndexPath

editStyleForRowAtIndexPath . Use if the table has several different editing styles.

canEditRowAtIndexPath . Use when some lines should be edited and some shouldn't.



So the correct way to implement a table delegate is:

- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == sss && indexPath.row == rrr)
    {
        return NO;
    }
    return YES;
}

      

+2


a source







All Articles