菜鸟教程小白 发表于 2022-12-12 14:57:50

ios - 向 UICollectionView 动态添加内容


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

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

<p>向collectionview添加新内容的代码:</p>

<pre><code>- (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 = ;

    // Add indexpath objects to the new array to be added to the collection view
    for (int i = resultsSize; i &lt; resultsSize + itemCnt; i++) {
      ];
    }
    // Update the collectionview
    ;
}
    completion:nil];
} else {
    ;
}
curPage++;
</code></pre>

<p>有什么明显的吗?它在功能上可以工作,但在所有闪烁和重新绘制时看起来很糟糕。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>好的,我自己解决了。我为 resultSize 变量使用了不正确的值。 for 循环应该从当前数组计数的末尾开始,并遍历我们添加的项目数,从而将它们添加到末尾。在构建 arrayWithIndexPaths 时,我从数组索引 1 的开头开始,导致它重新绘制整个 View 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 向 UICollectionView 动态添加内容,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/19030285/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/19030285/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 向 UICollectionView 动态添加内容