最近用UICollectionView做表格控件,自定義UICollectionViewLayout的遇到了一個崩潰杰刽。
*** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'UICollectionView received
layout attributes for a cell with an index path that does not exist:
<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}'
在UICollectionViewLayout的重載接口中我們需要定義
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
NSMutableArray<UICollectionViewLayoutAttributes *> *layoutAttributes = [NSMutableArray array];
/*計算需要展示的cell*/
/*
UICollectionViewLayoutAttributes
@property(nonatomic, strong) NSIndexPath *indexPath;
*/
return layoutAttributes;
}
在UICollectionView的delegate接口我們需要實現(xiàn)
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
// return SectionsNum
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
// return numberOfSection
}
崩潰的原因:
layoutAttributesForElementsInRect返回的UICollectionViewLayoutAttributes數(shù)組有indexPath沒有被 [NSIndexPath indexPathForRow:numberOfSection]覆蓋枢里。
換而言之
SectionsNum不小于layoutAttributes中任何一個UICollectionViewLayoutAttributes的indexPath.section枣接,
numberOfSection不小于layoutAttributes中任何一個UICollectionViewLayoutAttributes的indexPath.item做葵,