views:

5045

answers:

2

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?

A: 

Basically, you enable or disable editing using the methods

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

If editing is enabled, the red deletion icon appears, and a delete conformation requested to the user. If the user confirms, the delegate method

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

is notified of the delete request. If you implement this method, then swipe to delete is automatically made active. If you do not implement this method, then swipe to delete is not active, however you are not able to actually delete the row. Therefore, to the best of my knowledge, you can not achieve what you asked for, unless using some undocumented, private APIs. Probably this is how the Apple application is implemented.

unforgiven
I solved this by return the UITableViewCellEditingStyleDelete in tableView: editingStyleForRowAtIndexPath: if it's in editing mode.
willi
+18  A: 

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

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

Still need tableView:commitEditingStyle:forRowAtIndexPath: to commit the deletion.

willi
bu then swipe to delete is automatically enabled, again. Or not?
unforgiven
No, swipe to delete doesn't get enabled, if it's not in editing mode. That's why I return UITableViewCellEditingStyleNone as default.
willi
Forgot mention that you need if (editingStyle == UITableViewCellEditingStyleDelete) in commitEditingStyle:
willi