insertItemsAtIndexPaths更新错误 [英] insertItemsAtIndexPaths Update Error

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

问题描述

在UICollectionView中,我尝试使用 performBatchUpdates:completion 来执行网格视图的更新。我的数据源数组是 self.results

In UICollectionView, I am trying to use performBatchUpdates:completion to perform updates to my grid view. My data source array is self.results.

这是我的代码:

dispatch_sync(dispatch_get_main_queue(), ^{

            [self.collectionView performBatchUpdates:^{

                int resultsSize = [self.results count];
                [self.results addObjectsFromArray:newData];

                NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
                if (resultsSize == 0) {
                    [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:0 inSection:0]];
                }

                else {
                    for (int i = 0; i < resultsSize; i++) {
                        [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:resultsSize + i inSection:0]];
                    }
                }

                for (id obj in self.results)
                    [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];

            } completion:nil];

我所拥有/正在做什么的解释

当初始插入集合视图时,此代码运行正常。但是,当我在我的集​​合视图中添加/插入更多数据时(通过更新 self.results 并调用它),这会产生以下错误:

This code runs fine when the initial insertion into the collection view is done. However, when I add/insert more data into my collection view (by updating self.results and calling this), this gives the following error:


* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:无效
第0部分中的项目数更新后的
现有部分中包含的项目数(8)必须等于更新前该部分中包含的
项目数(4),加上或减去
从该部分插入或删除的项目数(32
已插入,0已删除)和加或减移入
或该部分之外的项目数(0移入,0移出)。 '

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (8) must be equal to the number of items contained in that section before the update (4), plus or minus the number of items inserted or deleted from that section (32 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

我理解这意味着数据源未正确更新。但是,当查询我的 self.results 数组时,我看到了新的数据计数。我在第一行使用 addObjectsFromArray 进行此操作。我还将旧结果大小存储在 resultsSize 中。我使用该变量将新添加的索引路径添加到 arrayWithIndexPaths

I understand this means that the data source wasn't updated properly. However, when querying my self.results array, I see the new count of the data. I am doing that on the first line using addObjectsFromArray. I also stored the old results size in resultsSize. I use that variable to add the newly added index paths into arrayWithIndexPaths.

现在添加/插入项目时,我尝试了以下for循环:

Now when adding/inserting items, I tried the following for-loops:

for(id obj in self.results)这就是我正在使用的现在。它最初工作但进一步插入崩溃。

for (id obj in self.results) which is what I am using now. It works initially but further inserts crashes.

for(UIImage * newData中的图像)最初工作原理但是进一步插入崩溃。

for (UIImage *image in newData) works as well initially but further inserts crash.

从函数名称,我相信 insertItemsAtIndexPaths 将在这些索引路径中插入所有项目没有循环。但是,如果没有循环,应用程序在最初尝试填充数据时会崩溃。

From the name of the function, I believe that insertItemsAtIndexPaths will insert all items at those index paths without a loop. However, without a loop, the app crashes when it initially tries to populate the data.

我还尝试从 resultsSize + 1 直到新的 self.results 计数(包含新数据)并且在初始更新时也会崩溃。

I also tried looping from resultsSize + 1 until the new self.results count (which contains the new data) and that also crashes at the initial update.

关于我做错的任何建议?

Any suggestions about what I'm doing wrong?

谢谢,

推荐答案

我在这里看到了一些错误。首先,我不确定你为什么使用dispatch_sync,我没有太多的GCD经验,我无法在那里使用它(它似乎挂起,并且UI没有响应)。也许其他人可以提供帮助。其次,在你添加索引路径的循环中,你循环遍历resultsSize,据我所知,它是更新前数组的大小,这不是你想要的 - 你想在以下处启动新索引resultsSize并循环到resultsSize + newData.count。最后,当您调用insertItemsAtIndexPaths时,您希望执行一次,而不是循环。我尝试了这个,它可以更新集合视图(我没有从头开始尝试使用空集合视图):

I see several things wrong here. First, I'm not sure why you're using dispatch_sync, I don't have much experience with GCD, and I couldn't make it work with that in there (it seemed to hang, and the UI was unresponsive). Perhaps someone else can help with that. Second, in the loop where you add index paths, you're looping over resultsSize, which as I understand it, is the size of the array before the update, which is not what you want -- you want to start the new indexes at resultsSize and loop up to resultsSize + newData.count. Lastly, when you call insertItemsAtIndexPaths, you want to do that once, not in a loop. I tried this, and it worked to update the collection view (I didn't try it from scratch with an empty collection view):

-(void)addNewCells {
    [self.collectionView performBatchUpdates:^{
        int resultsSize = [self.results count];
        [self.results addObjectsFromArray:newData];
        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
        for (int i = resultsSize; i < resultsSize + newData.count; i++) {
            [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }
            [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
    }
        completion:nil];
}

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

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