ios - 我想记住一个 (NSIndexPath *)indexPath 以便以后删除,但是崩溃了

标签 ios xcode uitableview memory

我这样定义一个成员:

@property (nonatomic, retain)  NSIndexPath *deletingIndexPath; //记录当前需要删除的行

并在

中赋值
- (void)tableView:(UITableView *)atableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    deletingIndexPath = indexPath;
    ...
}

并在另一个回调函数中使用它

NSArray *deleteIndexPaths = [NSArray arrayWithObjects:deletingIndexPath,nil];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];

然后它崩溃了...

在模拟器中它说

2012-07-10 16:56:54.887 p[20972:16a03] *** Assertion failure in -[NSIndexPath row], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableViewSupport.m:2606
2012-07-10 16:56:54.898 p[20972:16a03] Uncaught exception: Invalid index path for use with UITableView.  Index paths passed to table view must contain exactly two indices specifying the section and row.  Please use the category on NSIndexPath in UITableView.h if possible.

谁能告诉我为什么?

最佳答案

你在 ARC 吗?如果不是,您必须保留您的实例。您分配的那个在您可以使用之前自动释放。你可以使用 self.deletingIndexPath = indexPath; 这样属性就会保留实例

关于ios - 我想记住一个 (NSIndexPath *)indexPath 以便以后删除,但是崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11410465/

相关文章:

ios - 在 SpriteKit 中使用 AspectFit 显示边框外的内容

objective-c - Xcode 项目比较工具

ios - Swift 捕获 UITableViewCell 中的输入值

jquery - iScroll 自动循环?

ios - navigationItem 上的 UIImageView 尺寸错误

iphone - NSString 来自 [NSData dataWithContentsOfURL :]

iOS 可能的线程问题

ios - 如何在 iOS 中滚动 UITableView

ios - 启动和停止 indicatorView 并 swift 将其放在中心 tableView 中

ios - 当 textView isScrollEnabled = false 时使用 pinchGesture 调整字体和框架