我有一个 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/