无法让 deleteRowsAtIndexPaths 工作 [英] Cannot get deleteRowsAtIndexPaths to work

查看:24
本文介绍了无法让 deleteRowsAtIndexPaths 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView and a scheduler running in the background deleting records from the underlying data object. Every time an object is deleted I want the table view to update accordingly.

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

But i keep getting this annoying error message when trying to delete a row from the table view.

* 由于未捕获的异常NSInternalInconsistencyException 而终止应用程序,原因:用于UITableView 的索引路径无效.索引路径传递给表视图必须正好包含两个指定节和行的索引.如果可能,请使用 UITableView.hNSIndexPath 上的类别."

* Terminating app due to uncaught exception NSInternalInconsistencyException, reason: "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."

代码:

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]; 但这有不希望的副作用,擦除选定的索引.

PS: I have also tried [tableView reloadData]; but that has undesired side effects, erasing the selected index.

推荐答案

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

Have you considered following the message's advice? Your index path does not contain both a row and a section component. Use +[NSIndexPath indexPathForRow:inSection:] to create the index path object, so that it has the information the table view requires:

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

这篇关于无法让 deleteRowsAtIndexPaths 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆