UICollectionViewData indexPathForItemAtGlobalIndex 中的断言失败 [英] Assertion Failure in UICollectionViewData indexPathForItemAtGlobalIndex

查看:38
本文介绍了UICollectionViewData indexPathForItemAtGlobalIndex 中的断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using performBatchUpdates() to update my collection view, where I am doing a complete refresh, i.e. delete whatever was in it and re-insert everything. The batch updates are done as part of an Observer which is attached to a NSMutableArray (bingDataItems).

cellItems 是包含已插入或将要插入到集合视图中的项目的数组.

cellItems is the array containing items that are or will be inserted into the 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];
    }
}

我收到此错误,但不是所有时间(为什么?)

I get this error, but not all of the times (why ?)

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 断言失败,但它是不太清楚,即在 performBatchUpdates() 块中不建议执行 [collectionview reloadData].

I checked the only thread that mentioned the same problem here: UICollectionView Assertion failure, but it is not very clear i.e. doing [collectionview reloadData] is not advisable in the performBatchUpdates() block.

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

Any suggestions on what might be going wrong here ?

推荐答案

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

Finally! Ok, here's what was causing this crash for me.

如前所述,我正在创建补充视图,以便为我的集合视图提供自定义样式的部分标题.

As previously noted, I was creating supplementary views in order to provide custom-styled section headers for my collection view.

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

The problem is this: it appears that the indexPath of a supplementary view MUST correspond to the indexPath of an extant cell in the collection. If the supplementary view's index path has no corresponding ordinary cell, the application will crash. I believe that the collection view attempts to retrieve information for a supplementary view's cell for some reason during the update procedure. It crashes when it cannot find one.

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

Hopefully this will solve your problem too!

这篇关于UICollectionViewData indexPathForItemAtGlobalIndex 中的断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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