UIAlert用于删除tableView中的行

时间:2011-06-01 15:11:30

标签: ios objective-c cocoa-touch uitableview uialertview

我有这段代码:

- (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.
{

}
}

我想在取消tableView行之前显示提醒视图,然后将commitEditingStyle:(UITableViewCellEditingStyle)editingStyle的说明放在if方法代表的第一个UIAlert中...
有可能吗?

1 个答案:

答案 0 :(得分:5)

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

@interface MyClass : … {
    NSIndexPath *deleteIndexPath;
}
@end

在您的实施中:

- (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];
    }
}