UICollectionView 使用 performBatchUpdates 执行更新 [英] UICollectionView Performing Updates using performBatchUpdates

查看:34
本文介绍了UICollectionView 使用 performBatchUpdates 执行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView,我试图将项目动态/动画插入其中.所以我有一些异步下载图像的功能,并想批量插入项目.

I have a UICollectionView which I am trying to insert items into it dynamically/with animation. So I have some function that downloads images asynchronously and would like to insert the items in batches.

获得数据后,我想执行以下操作:

Once I have my data, I would like to do the following:

[self.collectionView performBatchUpdates:^{
    for (UIImage *image in images) {
        [self.collectionView insertItemsAtIndexPaths:****]
    }
} completion:nil];

现在代替 ***,我应该传递一个 NSIndexPaths 数组,它应该指向要插入的新项目的位置.我很困惑,因为在提供了位置之后,我该如何提供应该在该位置显示的实际图像?

Now in place of the ***, I should be passing an array of NSIndexPaths, which should point to the location of the new items to be inserted. I am very confused since after providing the location, how do I provide the actual image that should be displayed at that position?

谢谢

更新:

resultsSize 包含数据源数组的大小,self.results,在从 newImages 处的数据添加新数据之前.

resultsSize contains the size of the data source array, self.results, before new data is added from the data at newImages.

[self.collectionView performBatchUpdates:^{

    int resultsSize = [self.results count];
    [self.results addObjectsFromArray:newImages];
    NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];

    for (int i = resultsSize; i < resultsSize + newImages.count; i++)
          [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];

          [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];

} completion:nil];

推荐答案

参见 插入、删除和移动部分和项目来自iOS 集合视图编程指南":

要插入、删除或移动单个部分或项目,您必须遵循这些步骤:

To insert, delete, or move a single section or item, you must follow these steps:

  1. 更新数据源对象中的数据.
  2. 调用集合视图的适当方法来插入或删除部分或项目.

在通知您之前更新您的数据源至关重要任何更改的集合视图.集合视图方法假设您的数据源包含当前正确的数据.如果确实如此不是,集合视图可能会从您的数据源或要求不存在的项目并使您的应用程序.

It is critical that you update your data source before notifying the collection view of any changes. The collection view methods assume that your data source contains the currently correct data. If it does not, the collection view might receive the wrong set of items from your data source or ask for items that are not there and crash your app.

因此,在您的情况下,您必须先将图像添加到集合视图数据源,然后调用 insertItemsAtIndexPaths.然后集合视图会要求数据源委托函数为插入的项目提供视图.

So in your case, you must add an image to the collection view data source first and then call insertItemsAtIndexPaths. The collection view will then ask the data source delegate function to provide the view for the inserted item.

这篇关于UICollectionView 使用 performBatchUpdates 执行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆