2013-02-07 43 views
17

有誰知道如何重新加載/刷新UICollectionView集合視圖時顯示?基本上我正在尋找類似於UITableview的標準reloadData方法。刷新UICollectionview

+5

也許我誤解了,但UICollectionView實現了reloadData。 – danh

回答

45

您只需撥打[self.myCollectionView reloadData];個人部分和項目也可以重新加載:

[self.myCollectionView reloadSections:indexSet]; 
[self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths]; 
+9

WFIW我相信人們碰到這個問題的原因是因爲他們習慣於調用[myTableView reloadData],但是使用UICollectionViews必須調用[myCollectionView.collectionView reloadData]。 – capikaw

+0

[self.myCollectionView reloadData];只打一次電話。 想要重新載入所有數據。 –

9
[self.collectionView reloadData]; 
+0

雖然這確實會達到所需的結果,但使用reloadData會重繪所有單元格並影響性能。 – DrunkenBeard

+0

不是一個好主意,因爲重新加載整個桌子會導致不良的表現和能量。 – Karsten

0

正確的,最好的方法是使用

NSMutableArray *indexPaths = [NSMutableArray array]; 
//prepare some data 
//batchupdate as block 
[self.collectionView performBatchUpdates:^{ 
    //operations like delete 
    [self.collectionView deleteSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, count)]]; 
    [self.collectionView insertItemsAtIndexPaths:indexPaths]; 
} completion:^(BOOL finished) { 
    // call something when ready 
    } 
}]; 

和所有被預先計算一個很好的動畫。最佳做法是首先刪除並添加所有元素,以避免衝突。

+1

你救了我的一天!謝謝 –