2012-10-04 31 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