2014-03-07 111 views
1

我在基於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用新的數據?謝謝。

更新:此錯誤是因爲我使用了自定義佈局,並且它有一些錯誤的邏輯,現在它已修復。

+0

我以某種方式遇到類似的問題,產生相同的錯誤(接收不存在的索引路徑的佈局屬性)。我也使用一個自定義的佈局對象,我相信這個錯誤來自於這個對象。你能描述一下你的情況出了什麼問題嗎? – jchnxu

+1

@jchnxu:嗯這是很長的時間,基本上我嘗試了一個來自github.com的例子,並且錯誤在 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)矩形和 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath沒有正確地調用[self prepareLayout],但我再也找不到這個例子了,希望這會有所幫助。 – Tom

回答

2

加載集合視圖與表視圖非常相似。使用委託和數據源協議來處理大部分事務。

你情況類似

self.collectionView.dataSource = self; 

然後在數據源方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return self.imageArray.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    //Set up and return your cell 
} 

當你想改變內容,您可以替換或更新imageArray和reloadData的內容。

從API新的內容進來

[self.imageArray removeAllObjects]; 
[self.imageArray addObjectsFromArray:apiResponseArray]; 
[self.collectionView reloadData]; 

還有更多的也許有點高效率,但這應該讓你開始,其他的方法。一旦基本設置正確完成,您可以根據需要使用其他方法進行插入和批處理。

+0

這不行,奇怪。 – Tom

+0

由於未捕獲的異常'NSInternalInconsistencyException',原因:'UICollectionView收到了索引路徑不存在的單元的佈局屬性: {length = 2,path = 0 - 1}' – Tom

+0

第一個時間,API將返回20個對象,第二次將返回1個對象。 – Tom

相關問題