我想删除一个表格视图单元格,但在该操作发生之前我想给用户一个警报视图。我懂了:
- (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");
}
}
但是现在当我在单元格上向右滑动并出现删除按钮时,我没有得到 AlertView。当我按下删除按钮时,我只会得到 AlertView。当我按下删除按钮时,会出现消息,但单元格已被删除。
如何使这项工作?所以当我滑动时有一个 AlertView。