Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 379
Aran Mulholland Avatar asked Jan 22 '23 00:01

Aran Mulholland


2 Answers

- (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.

like image 56
Aran Mulholland Avatar answered Feb 05 '23 05:02

Aran Mulholland


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;
}
like image 33
jrtc27 Avatar answered Feb 05 '23 04:02

jrtc27