I want to delete multiple rows from a table view, based on the user\'s selection. Obviously I can\'t use the didSelectRowAtIndexPath
method because it will be calle
A late reply, but another way to handle this (if you are targeting iOS 5) is to toggle editing mode and use the built-in multiple cell selection. Easiest way to do this is to put this in viewDidLoad...
// add edit button to navigation bar, which auto toggles editing mode
self.navigationItem.rightBarButtonItem = self.editButtonItem;
// allows user to select multiple cells while in editing mode
self.tableView.allowsMultipleSelectionDuringEditing = YES;
See documentation for more details
You can do something this way:
- (void)tableView:(UITableView *)theTableView
didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedCellsMutableArray addObject:newIndexPath];
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedCellsMutableArray removeObjectIdenticalTo:newIndexPath];
}
}
When user press Delete Selected button - just invoke something like
// change your model here and then:
[yourView deleteRowsAtIndexPaths:selectedCellsMutableArray
withRowAnimation:UITableViewRowAnimationRight];