I want to delete a Table View cell, but before that action happens I want to give the user an alertview. I got this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Are you sure?"
delegate:self
cancelButtonTitle:@"NO"
otherButtonTitles:@"YES", nil];
[alert show];
[self.array removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Nee"])
{
NSLog(@"Nothing to do here");
}
else if([title isEqualToString:@"Ja"])
{
NSLog(@"Delete the cell");
}
}
But now when I swipe to right on the cell and the delete button appears I got no AlertView. I only get the AlertView when I press on the delete button. When I press on the delete button the message appears but the cell is already been deleted.
How to make this work? So there is an AlertView when I swipe.
Regarding the sequence, everything is fine.
commitEditingStylewill be called only when the delete button was pressed already. The point is that you are actually removing the object before the alert is responded to. Change it to this:Add this to the .m file before
@implementation:And then:
Edit: This should compile, despite minor syntax errors probably.
General assupmtion: You are dealing with one section only. At least only with one section within deletions are possible.