删除表视图中与核心数据关联的行
我的表视图是由包含核心数据中的数据的数组填充的,我想在相应更新核心数据的同时删除行,这是我的删除代码。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
NSManagedObject *selectedObject = [arrayList objectAtIndex:[indexPath row]];
[managedObjectContext deleteObject:selectedObject];
[arrayList removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
但是,当我点击删除按钮时,出现以下错误:
*** -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995 中的断言失败 *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新 (4) 后现有节中包含的行数必须等于行数更新之前该部分中包含的行数 (4),加上或减去从该部分插入或删除的行数(插入 0 行,删除 1 行)。'
我的数据源是 arrayList
,其中包含NSManagedObjects 从核心数据中获取。
有想法吗?提前致谢!
更新:
现在删除arrayList中的数据后可以进行删除,但是core data中的数据没有相应删除,当应用程序重新启动时,列表仍然是相同的。
My table view is populated from an array containing data in the core data, I want to delete rows while updating the core data correspondingly, here is my code of deletion.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
NSManagedObject *selectedObject = [arrayList objectAtIndex:[indexPath row]];
[managedObjectContext deleteObject:selectedObject];
[arrayList removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
However, I am getting the following error when I hit delete button:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
My data source is arrayList
containing NSManagedObjects fetched from core data.
Ideas? Thanks in advance!
Update:
Now the deletion can work after I remove the data in the arrayList, but data in core data didn't get deleted correspondingly, when app relaunches, the list is still the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着即使在您删除该行之后,数据源仍然返回 4 作为第 0 节中的行数。因此,现在表视图只为应该有 4 的部分显示 3 行。
根据您的描述,您可能需要除了从
CoreData
中删除对象之外,还可以从arrayList
中删除该对象。或者,从CoreData
删除对象后重新加载arrayList
,然后删除该行。It means that even after you deleted the row, the datasource is still returning 4 as the number of rows in section 0. So now the table view only has 3 rows displayed for a section that should have 4.
From your description, you probably need to delete the object from
arrayList
in addition to deleting it fromCoreData
. Alternatively reloadarrayList
after deleting the object fromCoreData
and then delete the row.