Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want something similar as the Alarm app, where you can't swipe delete the row, but you can still delete the row in Edit mode.

When commented out tableView:commitEditingStyle:forRowAtIndexPath:, I disabled the swipe to delete and still had Delete button in Edit mode, but what happens when I press the Delete button. What gets called?

like image 913
willi Avatar asked Jun 09 '09 10:06

willi


2 Answers

Ok, it turns out to be quite easy. This is what I did to solve this:

Objective-C

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

Swift 2

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {     if tableView.editing {          return .Delete     }      return .None } 

Swift 3

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {     if tableView.isEditing {         return .delete     }      return .none } 

You still need to implement tableView:commitEditingStyle:forRowAtIndexPath: to commit the deletion.

like image 110
willi Avatar answered Oct 16 '22 17:10

willi


Just to make things clear, swipe-to-delete will not be enabled unless tableView:commitEditingStyle:forRowAtIndexPath: is implemented.

While I was in development, I didn't implement it, and therefore swipe-to-delete wasn't enabled. Of course, in a finished app, it would always be implemented, because otherwise there would be no editing.

like image 41
Marc Rochkind Avatar answered Oct 16 '22 15:10

Marc Rochkind