objective-c - insertItemsAtIndexPaths 更新错误

标签 objective-c ios ipad nsarray uicollectionview

在 UICollectionView 中,我尝试使用 performBatchUpdates:completion 对我的 GridView 执行更新。我的数据源数组是 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];

解释我所拥有的/我正在做的:

当初始插入 Collection View 完成时,这段代码运行良好。但是,当我向我的 Collection View 中添加/插入更多数据时(通过更新 self.results 并调用它),会出现以下错误:

* 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 中。

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

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

for (UIImage *image in newData) 最初工作正常,但进一步插入崩溃。

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

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

对我做错了什么有什么建议吗?

谢谢,

最佳答案

我看到这里有几处错误。首先,我不确定你为什么要使用 dispatch_sync,我对 GCD 没有太多经验,我无法让它在那里工作(它似乎挂起,UI 没有响应)。也许其他人可以提供帮助。其次,在您添加索引路径的循环中,您正在遍历 resultsSize,据我所知,它是更新前数组的大小,这不是您想要的 - 您想要在以下位置启动新索引resultsSize 并循环到 resultsSize + newData.count。最后,当您调用 insertItemsAtIndexPaths 时,您希望执行一次,而不是循环执行。我试过了,它可以更新 Collection View (我没有用空 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];
}

关于objective-c - insertItemsAtIndexPaths 更新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665669/

相关文章:

c++ - 在 iOS 中,如何从 viewController 以外的类更改按钮的文本?

ios - 是否可以创建从plist读取的对象的可变版本

Xcode 4 版本 4A2002a

ios - 带 token 身份验证的 NSURLSession

iphone - 如何显示 CFStringType?喜欢 kABHomeLabel

objective-c - 编译器将如何解释此语句?

ios - 在 icecast 服务器中设置挂载点

ios - 无法呈现 View Controller (SLComposeViewController)

iphone - 导航栏上的自定义 UIBarButtonItem

ios - iPad 作为使用蓝牙的 Mac 键盘