【发布时间】:2012-10-04 17:34:54
【问题描述】:
我正在尝试使用以下代码更新UITableView:
NSMutableIndexSet *sectionsToDelete = [NSMutableIndexSet indexSet];
NSMutableIndexSet *sectionsToInsert = [NSMutableIndexSet indexSet];
NSMutableIndexSet *sectionsToReload = [NSMutableIndexSet indexSet];
/* ... */
[[self tableView] beginUpdates];
if ([sectionsToReload count]) {
DBGLogObject(sectionsToReload);
[[self tableView] reloadSections:sectionsToReload withRowAnimation:animation];
}
if ([sectionsToDelete count]) {
DBGLogObject(sectionsToDelete);
[[self tableView] deleteSections:sectionsToDelete withRowAnimation:animation];
}
if ([sectionsToInsert count]) {
DBGLogObject(sectionsToInsert);
[[self tableView] insertSections:sectionsToInsert withRowAnimation:animation];
}
[[self tableView] endUpdates];
日志:
sectionsToReload = <NSMutableIndexSet: 0x71b19f0>[number of indexes: 2 (in 1 ranges), indexes: (1-2)]
sectionsToInsert = <NSMutableIndexSet: 0x71ac570>[number of indexes: 3 (in 2 ranges), indexes: (0 3-4)]
(注意日志中没有sectionsToDelete。)
问题是即使没有删除我也会收到错误:
2012-10-04 19:21:16.769 Syntax[903:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:826
2012-10-04 19:24:51.655 Syntax[903:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete section 2, but there are only 2 sections before the update'
有什么想法吗?
【问题讨论】:
-
sectionToDelete 的日志显示什么?
-
没什么。它甚至不存在 - if ([sectionsToDelete count]) 评估为假。
-
我无法复制您的错误。当我添加您的代码时,我没有收到任何错误,但问题可能在于您如何更新数据源。您应该在执行此操作的位置发布整个方法,包括数据源更新。我不知道您如何在没有删除操作的情况下收到删除错误。
标签: ios objective-c uitableview cocoa-touch uikit