Thursday, June 6, 2013

UITableView allowsMultipleSelectionDuringEditing example in Objective C (iOS).

UITableView allowsMultipleSelectionDuringEditing

A Boolean value that controls whether users can select more than one cell simultaneously in editing mode.

@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

Discussion of [UITableView allowsMultipleSelectionDuringEditing]
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.

UITableView allowsMultipleSelectionDuringEditing example.
If you've subclassed UITableViewController (which you probably have), then you can simply do this:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
    // Set allowsMultipleSelectionDuringEditing to YES only while
    // editing. This gives us the golden combination of swipe-to-delete
    // while out of edit mode and multiple selections while in it.
    self.tableView.allowsMultipleSelectionDuringEditing = editing;

    [super setEditing:editing animated:animated];

Example of [UITableView allowsMultipleSelectionDuringEditing].
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {

    if ([controller.searchResultsTableView respondsToSelector:@selector(allowsMultipleSelectionDuringEditing)]) {
        controller.searchResultsTableView.allowsMultipleSelectionDuringEditing = YES;
    else {
        controller.searchResultsTableView.allowsSelectionDuringEditing = YES;

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

UITableView allowsMultipleSelectionDuringEditing example.
For a table view as shown in the picture, where one or more cells can be selected with a checkmark symbol, you have to set allowsMultipleSelectionDuringEditing = YES on the table view. This can be done either in viewDidLoad with

self.tableView.allowsMultipleSelectionDuringEditing = YES
or in the Attributes Inspector of the table view in the NIB/Storyboard file by setting "Editing" to "Multiple Selection During Editing".

The tableView:editingStyleForRowAtIndexPath: method is not needed for this.

(And btw your method returns UITableViewCellAccessoryCheckmark which is a UITableViewCellAccessoryType and not a UITableViewCellEditingStyle.)

End of UITableView allowsMultipleSelectionDuringEditing example article.