【问题标题】:Dynamically adding content to UICollectionView向 UICollectionView 动态添加内容
【发布时间】:2013-09-26 13:55:55
【问题描述】:

我有一个由 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++;

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

【问题讨论】:

    标签: ios uicollectionview collectionview


    【解决方案1】:

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

    【讨论】:

    • 您能告诉我如何解决这个问题吗?我也遇到了同样的问题,请告诉我你做了哪些代码更改
    • 你能告诉我解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多