OGeek|极客世界-中国程序员成长平台

标题: ios - 向 UICollectionView 动态添加内容 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:57
标题: ios - 向 UICollectionView 动态添加内容

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

这是可行的,但我的 collectionView 会闪烁并从顶部重新绘制整个 View 。我试图让它在 View 底部开始绘制新内容。

向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++;

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



Best Answer-推荐答案


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

关于ios - 向 UICollectionView 动态添加内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19030285/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4