有时(即使数据没有改变,它也不会每次都发生),我的 uiCollectionView 中的页眉/页脚无法实例化并且应用程序崩溃,因为我收到以下错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
当页脚位于奇数部分时,我将页脚的大小设置为 0。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view;
NSLog(@"indexpath section %ld", (long)indexPath.section);
if(kind == UICollectionElementKindSectionHeader){
HeaderViewQuestion *headerView = [collectView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewQuestion" forIndexPath:indexPath];
NSString *initial = [[NSString alloc] initWithFormat:@"Question %d : ",(int)indexPath.section+1];
NSMutableString *text = [[NSMutableString alloc] initWithString:initial];
[text appendString:[questions objectAtIndex:indexPath.section]];
NSInteger _stringLength=[text length];
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:text];
[attString addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica" size:20.0]
range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, _stringLength)];
headerView.label.attributedText = attString;
view = headerView;
}
if(kind == UICollectionElementKindSectionFooter){
FooterView *footerview = (FooterView*)[collectView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
footerview.addLine.tag = indexPath.section;
[footerview.addLine addTarget:self action:@selector(addLine:) forControlEvents: UIControlEventTouchUpInside];
view = footerview;
}
return view;
}
- (CGSize)collectionView:(UICollectionView *)collecView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
if(section%2 !=0)
{
return CGSizeZero;
}
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
return CGSizeMake(flowLayout.itemSize.width, 100);
}
- (CGSize)collectionView:(UICollectionView *)collecView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
return CGSizeMake(flowLayout.itemSize.width, 100);
}