objective-c - UICollectionViewData indexPathForItemAtGlobalIndex 断言失败

标签 objective-c ios uicollectionview

我正在使用 performBatchUpdates() 来更新我的 Collection View ,我正在那里进行完全刷新,即删除其中的所有内容并重新插入所有内容。批量更新是作为附加到 NSMutableArray (bingDataItems) 的 Observer 的一部分完成的。

cellItems 是包含已插入或将插入到 Collection View 中的项目的数组。

代码如下:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    cultARunner *_cultARunner = [cultARunner getInstance];
    if ( [[_cultARunner bingDataItems] count] ) {
        [self.collectionView reloadData];
        [[self collectionView] performBatchUpdates: ^{

            int itemSize = [cellItems count];
            NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];

            // first delete the old stuff
            if (itemSize == 0) {
                [arrayWithIndexPaths addObject: [NSIndexPath indexPathForRow: 0 inSection: 0]];
            }
            else {
                for( int i = 0; i < cellItems.count; i++ ) {
                    [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
                }
            }
            [cellItems removeAllObjects];
            if(itemSize) {
                [self.collectionView deleteItemsAtIndexPaths:arrayWithIndexPaths];
            }

            // insert the new stuff
            arrayWithIndexPaths = [NSMutableArray array];
            cellItems = [_cultARunner bingDataItems];
            if ([cellItems count] == 0) {
                [arrayWithIndexPaths addObject: [NSIndexPath indexPathForRow: 0 inSection: 0]];
            }
            else {              
                for( int i = 0; i < [cellItems count]; i++ ) {
                    [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
                }
            }
            [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
        }
        completion:nil];
    }
}

我得到这个错误,但不是所有的时间(为什么?)

2012-12-16 13:17:59.789 [16807:19703] *** Assertion failure in -[UICollectionViewData indexPathForItemAtGlobalIndex:], /SourceCache/UIKit_Sim/UIKit-2372/UICollectionViewData.m:442
2012-12-16 13:17:59.790 [16807:19703] DEBUG:    request for index path for global index 1342177227 when there are only 53 items in the collection view

我在这里检查了唯一提到同样问题的线程:UICollectionView Assertion failure ,但不是很清楚,即在 performBatchUpdates() block 中执行 [collectionview reloadData] 是不可取的。

关于这里可能出现的问题有什么建议吗?

最佳答案

终于!好的,这就是导致我崩溃的原因。

如前所述,我正在创建补充 View ,以便为我的 Collection View 提供自定义样式的节标题。

问题是这样的:补充 View 的 indexPath 似乎必须对应于集合中现存单元格的 indexPath。如果补充 View 的索引路径没有对应的普通单元格,应用程序将崩溃。我相信 Collection View 在更新过程中出于某种原因试图检索补充 View 单元格的信息。当找不到时它会崩溃。

希望这也能解决您的问题!

关于objective-c - UICollectionViewData indexPathForItemAtGlobalIndex 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13904049/

相关文章:

ios - 如何将测试数据添加到PreviewProvider

iphone - 限制父 View 内的 View

ios - UICollectionView 滚动到特定部分

ios - UICollectionViewCell 不出现

objective-c - 如何将此 subview 推送到它自己的子类中?

ios - 核心蓝牙外设每 30 秒断开一次

objective-c - 如何消除这样的 Objective-C @try @catch block ?

iphone - 下载文件时如何更新 UICollectionViewCell 子类中的进度条

objective-c - 如何等待动画师完成?

iphone - iOS 3.1.3 上的 NSMutableAttributedString