Pripyat
Pripyat

Reputation: 2937

UITableViewCell prevent deletion

I am looking for a way of preventing the deleting of one of my cells. (No delete button should appear next to the cell when the table view is in editing mode.)

How can this be made possible?

Upvotes: 2

Views: 1437

Answers (2)

Mika
Mika

Reputation: 5835

The accepted response works but is not the correct way to do it. There are two methods available: editingStyleForRowAtIndexPath and canEditRowAtIndexPath

editingStyleForRowAtIndexPath: Use when there are multiple different editing styles in the table

canEditRowAtIndexPath: Use when some rows should edit and some should not.

Therefore the correct way to implement your table delegate is:

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

Upvotes: 2

user121301
user121301

Reputation:

Implement editingStyleForRowAtIndexPath and return UITableViewCellEditingStyleNone for that row:

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

Upvotes: 7

Related Questions