UIAlert用于删除tableView中的行

Cra*_*Dev 2 cocoa-touch objective-c uitableview uialertview ios

我有这个代码:

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

[array removeObjectAtIndex:indexPath.row];

if (editingStyle == UITableViewCellEditingStyleDelete)
{
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)//OK button pressed
{

}
else if(buttonIndex == 1)//Annul button pressed.
{

}
}
Run Code Online (Sandbox Code Playgroud)

我想在取消一行之前显示警报视图tableView,然后commitEditingStyle:(UITableViewCellEditingStyle)editingStyle在方法委托的第一个if中添加指令UIAlert......
是否可能?

Vik*_*ica 5

将单元格的indexPath保存到ivar,并在警报视图调用的方法中使用该信息.

@interface MyClass : … {
    NSIndexPath *deleteIndexPath;
}
@end
Run Code Online (Sandbox Code Playgroud)

在您的实施中:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        deleteIndexPath = indexPath;
        //code for UIAlrtView
        // …
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)//OK button pressed
    {
        [array removeObjectAtIndex:deleteIndexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
Run Code Online (Sandbox Code Playgroud)