Iphone – Swipe to delete vs. edit button tapped on iPhone

iphoneuitableview

I set up my viewController to use the edit button in the way Apple recommends:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

When the user taps the edit button, it causes the setEditing:animated: method to trigger. In this method, I add or remove a "new row" depending on the editing value (i.e. a row the user can tap to add a new item).

When the user swipes across a cell (while not in edit mode), it also calls the setEditing:animated:, which causes my code to add this "new row" incorrectly. It should only show this new row when the entire viewController is in edit mode (i.e. when the edit button is tapped).

How can I resolve this issue?

Best Answer

If you implement tableView:willBeginEditingRowAtIndexPath: and tableView:didEndEditingRowAtIndexPath:, the behavior of the method calls will change...

It will no longer call setEditing:animated:, you must handle all editing change logic inside the above two methods. As a side effect of this, self.editing will no longer be YES when a swipe gesture is invoked. This is because the setEditing:animated: is responsible for setting self.editing = YES.