2013-09-26 127 views
2

我有一個UICollectionView由Web服務支持。我最初從服務中加載100個項目到集合視圖中。當用戶到達視圖的底部時,我用陰蒂加載下一個「頁面」的內容。動態添加內容到UICollectionView

這是工作,但我collectionView閃爍並重新繪製從頂部的整個視圖。我試圖讓它開始在視圖的底部繪製新內容。

代碼添加新的內容的CollectionView:

- (void)insertIntoCollectionView: (int) itemCnt { 

// only do the batchupdate for NON-initial load 

if(curPage != 0) { 
// Add items to main photos array, and update collectionview using batchUpdate 
    [self.collectionView performBatchUpdates:^{ 

    // Get starting size before update 
    int resultsSize = itemCnt; 

    // Create a new array to hold the indexpath for the inserted data 
    NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; 

    // Add indexpath objects to the new array to be added to the collection view 
    for (int i = resultsSize; i < resultsSize + itemCnt; i++) { 
     [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
    } 
    // Update the collectionview 
    [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; 
} 
    completion:nil]; 
} else { 
    [self.collectionView reloadData]; 
} 
curPage++; 

任何明顯?它在功能上起作用,但對於所有閃爍和重新繪製看起來都很糟糕。

回答

1

好的,設法自己修復它。我爲resultSize變量使用了不正確的值。 for循環應該在當前數組結束時開始,並循環遍歷我們添加的項目數,從而在最後添加它們。在構建arrayWithIndexPaths時,我從數組索引1的開始處開始,導致它重新繪製整個視圖。

+0

您能否讓我知道如何解決此問題?我也面臨同樣的問題,請讓我知道你做了什麼代碼更改 – Nishan29

+0

你能告訴我這個解決方案嗎? – Nishan29