删除行前确认

时间:2012-06-18 08:47:09

标签: ios uitableview uialertview

在尝试从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];
    }
}

因此我的两个日志都会返回正确的路径。

我的视图委托了UIAlertView

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

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

 *** -[NSIndexPath row]: message sent to deallocated instance 0x12228e00

5 个答案:

答案 0 :(得分:5)

您将需要保留indexPath,发生的情况是您的indexPath在解除警报时已经从您的系统中解除分配,

喜欢这个

更改

_tmpIndexPath = indexPath;

_tmpIndexPath = [indexPath retain];

答案 1 :(得分:3)

您可以尝试这样做

        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 setTag:indexPath.row];
        [alert show];

所以你可以得到值

[alertView tag]

clickedButtonAtIndex

答案 2 :(得分:1)

NSIndexPathNSObject,您可以在tableView: commitEditingStyle方法中对其进行自动释放,然后将其分配给您的实例变量:_tmpIndexPath = indexPath;稍后会将其释放。你需要做的是: _tmpIndexPath = [indexPath copy];但要小心,因为每次您负责在重新设置之前释放_tmpIndexPath。更清洁的解决方案是使用属性:

@property (nonatomic, copy) NSIndexPath *tmpIndexPath;
...
self.tmpIndexPath = indexPath; 

答案 3 :(得分:1)

承认上面的技术答案,我是否可以建议这实际上没有必要。如果您使用常规方法从表格视图中删除项目(编辑按钮并滑动该行),那么在流程中添加确认将与人们期望表格行为的方式相矛盾。用户在访问删除功能之前必须点击(或滑动),因此他们必须已经非常确定他们想要这样做。

答案 4 :(得分:0)

你使用ARC吗?如果没有尝试_tmpIndexPath = [indexPath retain];并且不要忘记稍后发布

相关问题