2013-08-25 40 views
0

通過更新表,有沒有區別,當我打電話讓我們說insertRowsAtIndexPaths更多次,1個對象的數組,比較調用一次,與所有對象的數組?UITableView批量更新行

這個問題同樣適用於重載和刪除,而不僅僅是插入

這可能看起來像一個簡單的問題,但更新機制是相當複雜的,特別是如果你混合插入,刪除,重新加載和不知道這是否會影響到動畫

[self.tableView beginUpdates]; 

[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:sectionIndex]] withRowAnimation:UITableViewRowAnimationFade]; 

[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:sectionIndex]] withRowAnimation:UITableViewRowAnimationFade]; 

[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:sectionIndex]] withRowAnimation:UITableViewRowAnimationFade]; 

[self.tableView endUpdates]; 

相較於

[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:sectionIndex], [NSIndexPath indexPathForRow:2 inSection:sectionIndex], [NSIndexPath indexPathForRow:3 inSection:sectionIndex]] withRowAnimation:UITableViewRowAnimationFade]; 

回答

2

只要您在begin/commitUpdate調用之間執行操作,就沒有區別。

的確切方法在TableView Programming Guide描述,在動畫塊中稱爲

刪除「操作和索引路徑的順序」和重裝操作部指定的行和段在原始表應該是刪除或重新加載;插入指定應將哪些行和部分添加到結果表中。用於標識部分和行的索引路徑遵循此模型。另一方面,插入或移除可變數組中的項目可能會影響用於連續插入或移除操作的數組索引;例如,如果您在某個索引處插入項目,則數組中所有後續項目的索引都會增加。

+0

你能支持這個嗎? –

+1

@Abizern是正確的。這就是這些操作*用於*的原因。 UICollectionView(更新)具有performBatchUpdates:completion:這是一個單一的方法,它用UITableView封裝你必須做的模式。 – greymouser