iphone - 无法使 deleteRowsAtIndexPaths 工作

标签 iphone objective-c xcode uitableview

我有一个 UITableView 和一个在后台运行的调度程序,从底层数据对象中删除记录。 每次删除对象时,我都希望 TableView 相应地更新。

但是当我尝试从 TableView 中删除一行时,我不断收到这个恼人的错误消息。

* 由于未捕获的异常 NSInternalInconsistencyException 而终止应用程序,原因:“与 UITableView 一起使用的索引路径无效。传递给 TableView 的索引路径必须恰好包含指定部分和行的两个索引。如果可能,请在 UITableView.h 中使用 NSIndexPath 上的类别。”

代码:

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:0];
NSUInteger row = [indexPath row];

NSMutableArray* files = [[NSMutableArray alloc] initWithArray:fileNames];
[files removeObjectAtIndex:[indexPath row]];
[fileNames dealloc];
fileNames = [NSArray arrayWithArray:files];

//[self.fileNames removeObjectAtIndex:row];
[self.tableDraft deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];

附言: 我也尝试过 [tableView reloadData]; 但这会产生不良副作用,会删除选定的索引。

最佳答案

您是否考虑过遵循消息的建议?您的索引路径不包含行和部分组件。使用 +[NSIndexPath indexPathForRow:inSection:]创建索引路径对象,以便它具有 TableView 所需的信息:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

关于iphone - 无法使 deleteRowsAtIndexPaths 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8774091/

相关文章:

iphone - 在 UITableViewCell 中对自定义 UIButton 进行动画处理

ios - 如何将图像从 Parse 加载到 UIImageView (iOS)

iPhone - 如何关闭屏幕并继续播放视频?

iphone - iOS缩放算法

iphone - Phonegap : How can I add a hash value to index. html 通过 Objective-C 在 webview 中?

iphone - 如何使 UITableViewCell 附件公开指示器在黑色背景中可见?

iphone - 导航栏标题字体大小

objective-c - 用于Objective-C的人脸检测API

用于从布局中扩充 View 的 iOS 模拟 (xib)

iphone - Xcode - 图像编辑