无法使deleteRowsAtIndexPaths正常工作

Dev*_*ull 5 iphone xcode objective-c uitableview

我有一个UITableView和后台运行的调度程序从基础数据对象中删除记录.每次删除对象时,我都希望表视图相应地更新.

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

*终止应用程序由于未捕获的异常NSInternalInconsistencyException,原因是:"无效的索引路径使用与UITableView传递到表视图必须包含两个指标指定部分和行索引路径请使用类别上.NSIndexPathUITableView.h可能的话."

代码:

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];
Run Code Online (Sandbox Code Playgroud)

PS:我也试过[tableView reloadData];但是有不良副作用,擦除了所选索引.

Jos*_*ell 21

您是否考虑过留言的建议?您的索引路径不包含行和节组件.使用+[NSIndexPath indexPathForRow:inSection:]创建索引路径对象,使之具有表视图需要的信息:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
Run Code Online (Sandbox Code Playgroud)

  • +1我同意......将所有这些发布在SO上需要的时间比阅读错误要长......它具体告诉你该做什么以及它为什么会中止...... (4认同)