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.
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?
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];
}
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
}
}
Heres a Swift version:
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if tableView.editing {
return UITableViewCellEditingStyle.Delete
}
return UITableViewCellEditingStyle.None
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With