2013-09-26 129 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