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

查看:200
本文介绍了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断言失败,但不是很清楚,即做 [collectionview reloadData] performBatchUpdates()块中不建议使用

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