4

I have a UIView subclass that has a dynamically created UICollectionView. Everything works fine in ios7 with displaying the headers just fine.

iOS 8 doesn't call this method at all, and the app crashes.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

If i comment out this method and I set to the header / footer size to 0, iOS 8 doesn't crash any more.

Here is the code to create the collection view:

int spacing = 39;

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(199, 60);
if (IS_IOS8) layout.estimatedItemSize = CGSizeMake(199, 60);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumInteritemSpacing = 1;
layout.minimumLineSpacing = 1;
layout.sectionInset = UIEdgeInsetsMake(1,1,1,1);
layout.headerReferenceSize = CGSizeMake(self.width - spacing*2, 50.0f);
layout.footerReferenceSize = CGSizeZero;

self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(spacing, 5, self.width - spacing*2, self.height - spacing*2) collectionViewLayout:layout];

self.collectionView.dataSource = self;
self.collectionView.delegate = self;

[self.collectionView registerClass:[OCEListCell class] forCellWithReuseIdentifier:CellIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderCellIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterCellIdentifier];
self.collectionView.backgroundColor = [UIColor clearColor];


[self addSubview:self.collectionView];

Data Source Methods:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView * view = nil;

    NSLog(@"viewForSupplementaryElementOfKind");
    if ([kind isEqualToString:UICollectionElementKindSectionHeader] )
    {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderCellIdentifier forIndexPath:indexPath];
        view.backgroundColor = [UIColor clearColor];

        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, view.width, 40)];
        lbl.textColor = [UIColor defaultTextColor];
        lbl.font = [UIFont bookFontOfSize:25.0f];
        lbl.numberOfLines = 1;
        lbl.text = indexPath.section == 0 ? @"Section 1 Header" : @"Section 2 Header";
        [view addSubview:lbl];
    }
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
    {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:FooterCellIdentifier forIndexPath:indexPath];
        view.backgroundColor = [UIColor clearColor];
    }

    NSLog(@"viewForSupplementaryElementOfKind:%@", view);
    return view;
}



-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size = CGSizeMake(collectionView.width, 50);
    NSLog(@"referenceSizeForHeaderInSection: %@", NSStringFromCGSize(size));
    return size;
}


-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    CGSize size = CGSizeZero;
    NSLog(@"referenceSizeForFooterInSection: %@", NSStringFromCGSize(size));
    return size;
}
jacob bullock
  • 651
  • 4
  • 14
  • What is the crash message? – shawnwall Oct 16 '14 at 15:02
  • unfortunately i am not actually even getting a stack trace. I have tried symbolic breakpoints and default exception handlers. I am using a library for for analytics and they show a crash log which is: Terminating app due to an uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' I am not calling insertObject anywhere in there so I wasn't quite sure if that was an accurate crash log – jacob bullock Oct 16 '14 at 15:07
  • 2
    I'm sure this is a bug. http://stackoverflow.com/questions/25460323/why-does-using-headerreferencesize-with-self-sizing-cells-in-a-collection-view-c – Adam Waite Oct 27 '14 at 10:46
  • Is there some other way to hack a header or footer into a uicollectionview with self-sizing cells? – webmagnets Feb 24 '15 at 22:19

1 Answers1

2

I just got back to looking at this again as I was able to get headers and footers working in different views awhile back and found out that the problem with this specific view was setting the estimateSize parameter.

Commenting out this line got rid of my troubles:

if (IS_IOS8) layout.estimatedItemSize = CGSizeMake(177, 60);

This was a frustrating thing to diagnose because no errors, compiler warnings, etc gave any information.

jacob bullock
  • 651
  • 4
  • 14