我在基於UICollectionView應用程序的工作,我通過加載 -UICollectionView - 刪除所有項目或更新以刷新它
NSUInteger newNumCells = [self.imageArray count];
NSIndexPath* newIndexPath;
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
[indexPaths removeAllObjects];
for (int i = 0; i < newNumCells; ++i) {
newIndexPath = [NSIndexPath indexPathForItem:i inSection:0];
[indexPaths addObject:newIndexPath];
}
self.indexPaths = indexPaths;
[self.collectionView insertItemsAtIndexPaths:indexPaths];
[self.collectionView reloadData];
它的工作好。
然後我在同一頁面有另一個搜索功能,所以當我從服務器獲得新的搜索結果時,self.imageArray內容發生了變化,我想刷新當前的UICollectionView,
例如,刪除所有項目,並從當前插入新項目self.imageArray,但是當我做刪除項目,總崩潰 -
[self.collectionView performBatchUpdates:^{
//self.indexPaths = [self.collectionView indexPathsForVisibleItems];
//[self.imageArray removeAllObjects];
self.indexPaths = [self.collectionView indexPathsForVisibleItems];
[self.imageArray removeAllObjects];
[self.collectionView deleteItemsAtIndexPaths:self.indexPaths];
[self.collectionView.collectionViewLayout invalidateLayout];
//[self.collectionView reloadData];
//[self.collectionView deleteItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
} completion:nil];
崩潰信息 -
Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2903.2/UICollectionViewData.m:341
2014-03-07 11:47:48.450 pixcell8[9089:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xb4a8430> {length = 2, path = 0 - 0}'
所以我想請問該怎麼辦刷新UICollectionView
用新的數據?謝謝。
更新:此錯誤是因爲我使用了自定義佈局,並且它有一些錯誤的邏輯,現在它已修復。
我以某種方式遇到類似的問題,產生相同的錯誤(接收不存在的索引路徑的佈局屬性)。我也使用一個自定義的佈局對象,我相信這個錯誤來自於這個對象。你能描述一下你的情況出了什麼問題嗎? – jchnxu
@jchnxu:嗯這是很長的時間,基本上我嘗試了一個來自github.com的例子,並且錯誤在 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)矩形和 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath沒有正確地調用[self prepareLayout],但我再也找不到這個例子了,希望這會有所幫助。 – Tom