在UICollectionViewData indexPathForItemAtGlobalInde

2019-07-05 12:18发布

我使用performBatchUpdates()来更新我的收藏看法,在这里我做一个完整的刷新,即删除无论是它,然后重新插入一切。 所述批处理更新作为被附接至观察者的部分完成NSMutableArraybingDataItems )。

cellItems是包含正在或将要插入到集合视图项目阵列。

下面是代码:

- (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断言失败 ,但它也不是很清楚,即做[collectionview reloadData]是不是在建议performBatchUpdates()块。

什么任何建议可能会错误吗?

Answer 1:

最后! 好吧,这里是什么原因导致这种崩溃的我。

正如前面提到的,我为了我的集合视图提供自定义风格的章节标题是创造的补充意见。

问题是这样的:它似乎补充视图的indexPath必须对应于集合中的现存小区的indexPath。 如果补充视图索引路径没有对应的普通细胞,应用程序会崩溃。 我相信,集合视图试图检索一个补充视图的小区信息的更新过程中由于某种原因。 它崩溃时,它不能找到一个。

希望这将解决你的问题呢!



Answer 2:

这是正确的解决方法,以这种崩溃:

您的意见补充各自与一定的指数路径相关联。 如果你没有在该索引路径的细胞(初始加载,你已经删除的行等),返回值为0的高度来通过布局的委托你的补充视图。

因此,对于流布局,实现UICollectionViewDelegateFlowLayout的

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

方法(和相应的页脚的方法,如果您使用页脚)用下面的逻辑

if ( you-have-a-cell-at-the-row-for-this-section )
    return myNormalHeaderSize;
else return CGSizeMake( 0,0 );

希望这可以帮助!



Answer 3:

reloadData不为我工作,因为使用的整个目的performBatchUpdates就是要改变动画。 如果你使用reloadData只刷新数据,但没有动画。

所以“代替建议performBatchUpdatesreloadData ”是相当多说:“放弃你想要做什么”。

对不起,我只是感到沮丧,因为这个错误不断来了我,而我试图做一些伟大的动画更新和我的模型是100%正确的,它的一些神奇的iOS里面得到打破,迫使我改变我完全的解决方案。

我的看法是,收集意见仍然马车,不能做复杂的动画刷新,即使他们应该可以。 因为这曾经是表视图同样的事情,但这些都是目前相当稳定(这需要时间,虽然)。

//编辑(2013年9月1日)的报告的bug,现在已经关闭,使这个问题似乎已经被苹果已经解决了。



Answer 4:

我一直有同样的问题。

我已经尝试了许多变化,但似乎工作的最后一个是[self.collectionView reloadData]其中"self.collectionView"是您的收藏视图的名称。

我曾尝试以下方法,直接从“UICollectionView类参考”:插入,移动和删除项目。

这些都是在第一次使用时,以“动”的项目从一个部分到另一个。

  • deleteItemsAtIndexPaths:

  • insertItemsAtIndexPaths:

接下来,我想moveItemAtIndexPath:toIndexPath:

他们都将产生以下错误:

在断言失败 - [UICollectionViewData indexPathForItemAtGlobalIndex:],/SourceCache/UIKit_Sim/UIKit-2372/UICollectionViewData.m:442

因此,尝试“reloadData”的方法。



Answer 5:

如果从包含页眉/页脚一节中删除最后一个单元格出现的错误。

我想在那个时候返回页眉/页脚大小/元素零,这有时会修复该问题。

选项:

  1. 刷新整个表视图,而不是动画拆除的最后一项。
  2. 增加一个额外的不可见的,基本单元的尺寸小于1。


Answer 6:

一个cheeseball错误,可能导致此错误重复使用相同的UICollectionViewFlowLayout对同一视图 - 控制多个collectionViews! 只是初始化不同flowLayouts每个的CollectionView,你会好到哪里去!



Answer 7:

我就遇到了这个问题,当我从我的收藏视图中删除的小区之一。

问题是,我使用了一个自定义布局,呼叫layoutAttributesForElementsInRect返航比删除后的集合视图细胞的数量更多。

显然UICollectionView通过而不检查细胞的数量由该方法返回的数组只是迭代。

修改方法返回相同数量的布局属性的解决了崩溃。



Answer 8:

我仍然无法弄清楚全球指数是如何递增的那么多,但我通过底层数据源阵列即插入一个临时项目解决我的问题cellItems并调用[self.collectionview reloadData]viewDidLoad()

这将暂时插入一个占位符细胞在收集视图,直到我触发使用实际进程performBatchUpdates()



文章来源: Assertion Failure in UICollectionViewData indexPathForItemAtGlobalIndex