2012-10-04 34 views
3

我想用下面的代码更新UITableView的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' 

任何想法?

+0

行或区段的号码是什么日志sectionsToDelete的表现? – rdelmar

+0

什么都没有。它甚至不在那里 - 如果([sectionsToDelete count])评估为false。 –

+0

我无法复制您的错误。当我添加你的代码时,我没有遇到任何错误,但问题可能在于你如何更新你的数据源。您应该在完成此操作的位置发布整个方法,包括数据源更新。我不知道如何在没有删除的情况下获得删除错误。 – rdelmar

回答

0

“清洁生成文件夹”,现在它可以像预期的那样工作。我不知道为什么。

3

实际上,现在我已经复制了您的错误 - 如果您的数据在此更新之前只有2个部分(这是错误消息所暗示的),那么您无法重新加载第1部分和第2部分。更新0和1?我猜测,当重新加载发生时,它必须先删除旧数据并添加新数据,这就是删除错误信息的来源。

+0

我做了“清洁生成文件夹”,现在它工作。不管怎样,谢谢! –

0

你应该改变你的数据源,如果你删除的行或部分,你应该改变的tableView