什么可能导致错误“断言失败 - [UICollectionViewData numberOfItemsBeforeSection:]”?

时间:2016-07-18 08:33:32

标签: ios uicollectionview nsindexset

这是我的代码段:

NSIndexSet *indexSet =  self.selectedIndexSet;
__block SGPhotoSelectorCell *cell;

[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {

    NSLog(@"%lu", (unsigned long)idx);
    ***NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:idx];***

    cell = (SGPhotoSelectorCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.numLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)idx];

    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}];

出现此错误的原因是什么?

  

“断言失败 - [UICollectionViewData numberOfItemsBeforeSection:]”

1 个答案:

答案 0 :(得分:0)

我认为如果你小心这个问题很简单。请再看一下坠机的原因。 “断言失败 - [UICollectionViewData numberOfItemsBeforeSection:]”,意思是找不到该项的部分,所以你应该使用“NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];”替换“ * NSIndexPath indexPath = [NSIndexPath indexPathWithIndex:idx]; ”。 Okey,Bingo