删除行前确认

wou*_*_be 4 uitableview uialertview ios

我试图UIAlertView在实际删除一个单元格之前显示一个UITableView

NSIndexPath *_tmpIndexPath;


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

         NSLog(@"%d", indexPath.row); // 2
         NSLog(@"%d", _tmpIndexPath.row); // 2

        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Dete" message:@"Are you sure you want to delete this entry?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil] autorelease];
        [alert show];
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的日志都会返回正确的路径.

我有我的视图委托UIAlertView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%d", _tmpIndexPath.row);
    if(buttonIndex == 1)
    {
        NSLog(@"%d", _tmpIndexPath.row);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我无法弄清楚为什么clickButtonAtIndex()我在尝试登录时遇到错误_tmpIndexPath.row

 *** -[NSIndexPath row]: message sent to deallocated instance 0x12228e00
Run Code Online (Sandbox Code Playgroud)

Oma*_*ith 5

你将需要保留indexPath,发生的事情是你的indexPath在警报被解除时已经从你的系统中释放了,

像这样

更改

_tmpIndexPath = indexPath;
Run Code Online (Sandbox Code Playgroud)

_tmpIndexPath = [indexPath retain];
Run Code Online (Sandbox Code Playgroud)