4

我试图UIAlertView在实际从 a 中删除一个单元格之前显示 aUITableView

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
4

5 回答 5

5

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

像这样

改变

_tmpIndexPath = indexPath;

_tmpIndexPath = [indexPath retain];
于 2012-06-18T08:50:37.990 回答
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

于 2012-06-18T08:57:31.527 回答
1

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

于 2012-06-18T08:58:52.513 回答
1

NSIndexPath is a NSObject and you have it autoreleased in your tableView: commitEditingStyle method so assigning it to your instance variable: _tmpIndexPath = indexPath; it gets deallocated later. What you need to do is: _tmpIndexPath = [indexPath copy]; but be careful because every time you are responsible to release your _tmpIndexPath before setting it again. The cleaner solution is to use properties:

@property (nonatomic, copy) NSIndexPath *tmpIndexPath;
...
self.tmpIndexPath = indexPath; 
于 2012-06-18T08:54:34.197 回答
0

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

于 2012-06-18T08:52:04.120 回答