Objective-c – swipe to delete when already in edit mode

cocoa-touchiphoneobjective cuitableview

I have an iphone app using a uitableview where I'd like the "reorder" controls to always be displayed, and have the user swipe to delete rows.

The approach I'm currently taking is to put the tableview in edit mode and allow selection in edit mode

self.tableView.editing = YES;
self.tableView.allowsSelectionDuringEditing = YES;

I then hide the red delete circles using

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

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

I cant figure out how to get the swipe gesture to bring up the "delete" on the right side when the tableview is already in edit mode, can somebody point me in the right direction?

alternatively, if someone knows how to get the cell reordering controls to show when NOT in edit mode, that would also be a workable solution

cheers

Best Answer

When the user swipes on a given row, you need to store a reference somewhere so that you can change the value returned by editingStyleForRowAtIndexPath and shouldIndentWhileEditingRowAtIndexPath. Your best bet is likely to use indexPathForCell on the cell that is swiped and store that. Then in the two display methods above you need to check if the NSIndexPath is the same or not (I'm not sure if they will be the same pointer or if you'll need to compare the section/row values - testing required). If they match, display the delete button.

Note that you may need to call reloadData on your tableView to have the effect appear without scrolling away and back again.

Related Topic