UICollectionView and Supplementary View (header)

前端 未结 10 1096
南旧
南旧 2021-02-04 05:17

Trying to add a a Supplementary view into my UICollectionView as a header. I\'m having issues getting it to work.

I use a custom UICollectionViewFlowL

10条回答
  •  借酒劲吻你
    2021-02-04 05:27

    I cleaned up my code and removed the UICollectionView I had created in IB and created it all in code. I ran the it again and got a different error and realized I didn't set the delegate or dataSource in IB. I did:

    self.collectionView.delegate = self;
    self.collectionView.datasource = self;
    

    But that must not have been good enough.

    So with UICollectionView created in IB and not setting the delgate/datasource in IB, but rather in the code:

    [self.view bringSubviewToFront:self.collectionView];
    self.collectionView.collectionViewLayout = collectionViewFlowLayout;
    self.collectionView.backgroundColor = [UIColor orangeColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[DatasetCell class] forCellWithReuseIdentifier:@"cvCell"];
    
    [self.collectionView registerClass:[CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
    

    was an issue. I redid it to create the UICollectionView all in code:

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewFlowLayout];
    collectionView.translatesAutoresizingMaskIntoConstraints = FALSE;
    collectionView.backgroundColor = [UIColor yellowColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    [collectionView registerClass:[DatasetCell class] forCellWithReuseIdentifier:@"cvCell"];
    
    [collectionView registerClass:[CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
    
    [self.view addSubview:collectionView];
    
    self.collectionView = collectionView;
    

    and i get a different error:

    *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UICollectionView.m:2249
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindSectionHeader with identifier CollectionHeaderView - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
    

    which I'll try to figure out.

    EDIT:

    figured it out, I was registering the header incorrectly:

    [collectionView registerClass:[CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
    

    switched to:

    UINib *headerNib = [UINib nibWithNibName:@"CollectionHeaderView" bundle:nil];
    
    [collectionView registerNib:headerNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
    

    and everything works. Not entirely sure how the registerClass: wasn't working though.

提交回复
热议问题