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

标签 objective-c swift uitableview uialertview uialertcontroller

我想删除一个 TableView 单元格,但在该操作发生之前我想给用户一个警告 View 。我明白了:

- (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。

最佳答案

关于顺序,一切都很好。 commitEditingStyle 仅在已按下删除按钮时才会调用。关键是您实际上是在响应警报之前删除对象。改成这样:

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

@interface PutYourViewControllerClassNameHere
@property (strong, nonatomic) NSIndexPath *indexPathToBeDeleted;
@end

然后:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        self.indexPathToBeDeleted = indexPath;

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Are you sure?"
                                                       delegate:self
                                              cancelButtonTitle:@"NO"
                                              otherButtonTitles:@"YES", nil];
        [alert show];
        // 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.     
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 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) 

    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"NO"])
    {
        NSLog(@"Nothing to do here");
    }
    else if([title isEqualToString:@"YES"])
    {
        NSLog(@"Delete the cell");

        [self.array removeObjectAtIndex:[self.indexPathToBeDeleted row]];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPathToBeDeleted] withRowAnimation:UITableViewRowAnimationFade];
    }
}

编辑:这应该可以编译,尽管可能存在轻微的语法错误。 一般假设:您只处理一个部分。至少只有一个部分可以删除。

关于objective-c - 删除 UITableView 单元格之前发出警报 - (UIAlertController) Swift 或 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297738/

相关文章:

objective-c - NSPredicate 对复杂对象数组的过滤

ios - 绘制矩形 :withAttributes dies with "message sent to deallocated instance"

objective-c - 使用 ImageView 快速连续显示多张图像

iphone - 如何在ios上实现360度展示案例?

ios - 在 iOS 上使用 Swift 将 NaN 存储在 Firebase 中

swift - '+' 已弃用 : Mixed-type addition is deprecated in Swift 3. 1

iphone - 从其他控件继承的值

iOS范围栏字体大小?

swift - tableView.beginUpdates() 行为异常

ios - Xcode随机变量不起作用