Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Swipe-to-Delete Gesture in UITableView

Tags:

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.

like image 408
ArtSabintsev Avatar asked Nov 28 '11 20:11

ArtSabintsev


4 Answers

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?

like image 87
aopsfan Avatar answered Nov 28 '22 08:11

aopsfan


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];
}
like image 25
SimplyKiwi Avatar answered Nov 28 '22 08:11

SimplyKiwi


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
    }
}
like image 41
Teodor Ciuraru Avatar answered Nov 28 '22 10:11

Teodor Ciuraru


Heres a Swift version:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if tableView.editing {
        return UITableViewCellEditingStyle.Delete
    }
    return UITableViewCellEditingStyle.None
}
like image 41
Esqarrouth Avatar answered Nov 28 '22 09:11

Esqarrouth