Wednesday, January 11, 2012

iOS 5 Dev Warning: UITableView's Multiple Select During Editing Doesn't Work with Swipe to Delete

I attended Session 125 - UITableView Changes, Tips, Tricks at WWDC 2011. I also downloaded the video & slides of that session from the WWDC 2011 Session Videos site  (Apple developer account required) .

You can here it in the video, and being in the room, one of the, if not the, favorite moment for developers at the talk was when multiple select during editing was announced. The announcement starts at 16:51 in the talk video.

Jason Beaver immediately shows Apple's Mail application in edit mode. Why? Because that's what every developer and user that has spent any time in Mail want in their apps when editing lists of user content. 

You would expect, given the heavy references to the Mail app and how it works that what regular developers would now have in UITableView is exactly the functionality Mail supports.

But You Would Be Wrong Because Swipe to Delete is Disabled!
If you do this in your code:

self.tableView.allowsMultipleSelectionDuringEditing = YES;
or change the UITableView property in the XIB for Editing to use Multiple Selection During Editing you don't get swipe to delete.

I have no idea if that functionality was planned at WWDC and got dropped during development. The only hint you get swipe to delete won't work in the iOS 5 release documentation is the following on the allowsMultipleSelectionDuringEditing property (emphasis mine):
The default value of this property is NO. If you it to YES, check marks appear next to selected rows in editing mode. In addition, UITableView does not query for editing styles when it goes into editing mode. If you call indexPathsForSelectedRows, you can get the index paths that identify the selected rows.
So if you don't see the docs, or it just doesn't click on first read that this means no swipe to delete, you might do a whole bunch of work for no reason because you need both multiple select during edit and swipe to delete, just...like...Mail.app. I'm only saying this for a friend, of course.

Skeptical? I was too, went through all 7 stages of grief before conclusively proving it. I kept thinking: How could Apple play Lucy with the multiple select football to my Charlie Brown?

Prove it to yourself. Go download Apple's TableMultiSelect sample (Apple developer account required). Open MyTableViewController.m and paste the following three lines in (I pasted after the tableView:cellForRowAtIndexPath: method just to be tidy):

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"Commit Editing Style");
}

Run your app in the simulator and true to swipe to delete. It won't work.


Now go to line 74 and comment self.tableView.allowsMultipleSelectionDuringEditing = YES; 


BOOM, you got swipe to delete!


Next Steps
I know, file a radar or GTFO and I will, eventually. I say this not to deride Apple. UITableView is a great piece of UI programming. Whenever designing something of my own, I always ask:
What Would UITableView Do?
I wrote this to warn developers using UITableView's multiple selection functionality at the time of this writing (Xcode 4.2.1 & iOS 5.0.1) is an either or choice with swipe to delete that user's are most likely not going to be happy with. Better to stick with custom implementations unless/until Apple changes the functionality.


Update
An workaround has been found. Default your tableView to not allow multiple select during editing so you get swipe to delete. Then in your viewController's setEditing: method, enable multiple select on your tableView. When editing is done, set your tableView back to not allow multiple select during editing.


Seems like a hack, but I've had a bunch of people tell me it works!