20

Is it possible to disable the 'swipe-to-delete' gesture for a row/cell in a tableView? If so, how do you do it? The cell should still be editable in EDIT mode, but the swipe-to-delete gesture should be disabled.

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71
  • 2
    Is it the same question as [this](http://stackoverflow.com/questions/969313/uitableview-disable-swipe-to-delete-but-still-have-delete-in-edit-mode)? – Sergey Kalinichenko Nov 28 '11 at 20:42

4 Answers4

43

Here's what to do:

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Detemine if it's in editing mode
    if (self.tableView.editing) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}

You still need tableView:commitEditingStyle:forRowAtIndexPath: to animate the deletion.

This is a much cleaner solution than iBrad Apps' solution, since you can use the default self.editButtonItem instead of a custom button.

Link: UITableView disable swipe to delete, but still have delete in Edit mode?

Community
  • 1
  • 1
aopsfan
  • 2,451
  • 19
  • 30
  • @dasblinkenlight already provided this link. No point of re-posting code from another thread. Its up to the OP now since we gave him both ways to do it :) – SimplyKiwi Nov 28 '11 at 21:34
  • Yup, I implemented this and it worked. I used @dasblinkenlight's link. Thanks! – ArtSabintsev Nov 28 '11 at 21:53
  • I don't understand. Where is the cell index which doesn't want to have the Swipe to Delete gesture? The question pointed out "one cell/row to have it disabled". – Teodor Ciuraru Jul 08 '15 at 11:22
  • @TeoC. I didn't reference the index path because according to the question, *all* rows in the table were barred from swipe-to-delete. If instead you'd like to disable some rows from all editing (including swipe-to-delete), try iBrad Apps' solution, returning a condition for the index path instead of `NO`. – aopsfan Jul 08 '15 at 15:35
14

Yes. The code below will disable it.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

Response to your comment:

Edit2: My code below will work better with a custom button. If you want the default button then go to the link that @dasblinkenlight posted.

So pretty much make a button where you want the edit buttons to show and then call this method.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableview setEditing:editing animated:animated];
}
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • 1
    The cell should still be editable in EDIT mode, but the swipe-to-delete gesture should be disabled. That's what I need. – ArtSabintsev Nov 28 '11 at 20:47
  • Ok you should reword your post then. The link that @dasblinkenlight showed you should do that then. – SimplyKiwi Nov 28 '11 at 20:49
  • Thanks, edited the post. Will look into @dasblinkenlight's post in a second. – ArtSabintsev Nov 28 '11 at 20:50
  • Hmmm...making a button which acts like the `editButtonItem` in all ways but one seems like a bad idea, especially considering how cumbersome it can be...why do all this when there is a supported way to do what you need? – aopsfan Nov 28 '11 at 21:28
  • I updated my post, this is more if you want to use a custom button (maybe OP wants custom button?). I provided OP with direction if he wants to use traditional ways. – SimplyKiwi Nov 28 '11 at 21:37
  • @iBradApps Thanks for the solution. I'm not using a custom button. but I understand why you may have thought that I was using one. For reasons that I rather not go into, I needed the gesture to be disabled, but cell-deletion to still be achievable in the alternate, two-step process (e.g., red accessory-icon on the left that brings up the 'delete' button onClick). Thanks again! – ArtSabintsev Nov 28 '11 at 21:57
1

If you don't want to allow certain cells to be swiped and deleted, here's a solution:

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

Also don't forget to have this implemented in order for it to work:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // do something when deleted
    }
}
Teodor Ciuraru
  • 3,417
  • 1
  • 32
  • 39
1

Heres a Swift version:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if tableView.editing {
        return UITableViewCellEditingStyle.Delete
    }
    return UITableViewCellEditingStyle.None
}
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168