在删除UITableView单元格之前发出警报 – (UIAlertController)Swift或Objective-C

前端之家收集整理的这篇文章主要介绍了在删除UITableView单元格之前发出警报 – (UIAlertController)Swift或Objective-C前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想删除一个表视图单元格,但在该操作发生之前,我想给用户一个警报视图.我懂了:
  1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  2. if (editingStyle == UITableViewCellEditingStyleDelete) {
  3. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
  4. message:@"Are you sure?"
  5. delegate:self
  6. cancelButtonTitle:@"NO"
  7. otherButtonTitles:@"YES",nil];
  8. [alert show];
  9.  
  10. [self.array removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
  11. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  12. }
  13. }
  14.  
  15. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  16. {
  17. NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
  18. if([title isEqualToString:@"Nee"])
  19. {
  20. NSLog(@"Nothing to do here");
  21. }
  22. else if([title isEqualToString:@"Ja"])
  23. {
  24. NSLog(@"Delete the cell");
  25.  
  26. }
  27. }

但是现在当我在单元格上向右滑动并且删除按钮出现时,我没有AlertView.当我按下删除按钮时,我只获得AlertView.当我按下删除按钮时,会出现消息,但单元格已被删除.

如何使这项工作?所以当我滑动时有一个AlertView.

关于序列,一切都很好.只有在按下删除按钮时才会调用commitEditingStyle.关键是你实际上在响应警报之前删除了对象.把它改成这个:

在@implementation之前将其添加到.m文件中:

  1. @interface PutYourViewControllerClassNameHere
  2. @property (strong,nonatomic) NSIndexPath *indexPathToBeDeleted;
  3. @end

然后:

  1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  2. if (editingStyle == UITableViewCellEditingStyleDelete) {
  3.  
  4. self.indexPathToBeDeleted = indexPath;
  5.  
  6. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
  7. message:@"Are you sure?"
  8. delegate:self
  9. cancelButtonTitle:@"NO"
  10. otherButtonTitles:@"YES",nil];
  11. [alert show];
  12. // do not delete it here. So far the alter has not even been shown yet. It will not been shown to the user before this current method is finished.
  13. }
  14. }
  15.  
  16. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  17. {
  18. // This method is invoked in response to the user's action. The altert view is about to disappear (or has been disappeard already - I am not sure)
  19.  
  20. NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
  21. if([title isEqualToString:@"NO"])
  22. {
  23. NSLog(@"Nothing to do here");
  24. }
  25. else if([title isEqualToString:@"YES"])
  26. {
  27. NSLog(@"Delete the cell");
  28.  
  29. [self.array removeObjectAtIndex:[self.indexPathToBeDeleted row]];
  30. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPathToBeDeleted] withRowAnimation:UITableViewRowAnimationFade];
  31. }
  32. }

编辑:这应该编译,尽管可能有轻微的语法错误.一般说法:您只处理一个部分.至少只有删除内的一个部分是可能的.

猜你在找的Swift相关文章