2

我有一个由 Web 服务支持的 UICollectionView。我最初将 100 个项目从服务加载到集合视图中。当用户到达视图底部时,我会动态加载下一个“页面”内容。

这是可行的,但我的 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++;

有什么明显的吗?它在功能上有效,但在所有闪烁和重新绘制时看起来很糟糕。

4

1 回答 1

1

好的,设法自己修复它。我为 resultSize 变量使用了不正确的值。for 循环应该从当前数组计数的末尾开始,并遍历我们添加的项目数,从而将它们添加到末尾。在构建 arrayWithIndexPaths 时,我从数组索引 1 的开头开始,导致它重新绘制整个视图。

于 2013-09-26T18:27:51.647 回答