I have an UITableViewController, set up so that users can delete rows. This is my code for the handler of the delete action, and the delegate method for the UIAlertView:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"t" message:@"del?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
if (buttonIndex != [alertView cancelButtonIndex]) {
//my code to delete from the data source is here. it works fine.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationFade];
}
}
The problem is in that last line where it’s supposed to actually delete the rows from the tableView. If I put that (and the code that deletes from the data source) in the first method instead of the UIAlertView stuff, it works fine.
What is the proper way of doing this?
The problem was that the
UIIndexPathobject was wrong inside that method! I guess it’s because there is no row selected while the alert view is showing. So, I saved it before doing theUIAlertViewcode into a property of the view controller class, and retrieved it later in the second method, and it works fine.