Aran Mulholland
Aran Mulholland

Reputation: 23935

UITableViewCell: Allowing Selective Deletion

I have a table view and want to allow reordering of all cells, however there are certain cells that I do not want to be allowed to be deleted. when the UiTableView is put into deletion mode I do not want the red '-' button to appear on the left hand side, and do not want the swipe gesture to bring up the Delete button of these cells but want it to happen for the others. Any ideas?

Upvotes: 4

Views: 1072

Answers (2)

Aran Mulholland
Aran Mulholland

Reputation: 23935

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    //if we cant delete the object represented at the index path
    if ([[tableViewObjectsArray objectAtIndex:indexPath.row] canBeDeleted] == NO){
        return UITableViewCellEditingStyleNone;
    }
    //otherwise allow the deletion
    else{
        return UITableViewCellEditingStyleDelete;
    }
}

Of course this leaves an empty space where the '-' button should be, but it does not allow deletion. And also does not allow the swipe deletion either.

Upvotes: 7

jrtc27
jrtc27

Reputation: 8526

implement:

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

Upvotes: 2

Related Questions