2016-07-18 68 views
0

這裏是我的代碼片段:什麼會導致錯誤「聲明失敗 - [UICollectionViewData numberOfItemsBeforeSection:]」?

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:]」

回答

0

我覺得這個問題很簡單,如果你很小心。請再看看崩潰的原因。 「聲明失敗 - [UICollectionViewData numberOfItemsBeforeSection:]」,含義是項目的部分未找到,所以你應該使用「NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];」替換「* NSIndexPath indexPath = [NSIndexPath indexPathWithIndex:idx];」。奧基,賓果

相關問題