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

查看:6754
本文介绍了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编程指南:

See Inserting, Deleting, and Moving Sections and Items from the "Collection View Programming Guide for iOS":


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

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


  1. 更新数据源对象中的数据。

  2. 调用集合视图的适当方法是插入或删除部分或项目。

在通知之前更新数据源至关重要
collection vi任何变化。集合视图方法假设您的数据源包含当前正确的数据
。如果没有
,则集合视图可能会从
您的数据源收到错误的项目集,或者询问不存在的项目并使您的
应用程序崩溃。

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天全站免登陆