The code below displays my header view correctly, but for each of the sections in the UICollectionView:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeaderCollectionReusableView" forIndexPath:indexPath]; switch (indexPath.section) { case Section_One: return headerView; case Section_Two: return headerView; case Section_Three: return headerView; case Section_Four: return headerView; case Section_Five: return headerView; default: return headerView; } }
What I would like to do instead, is not display a header view for 'Section_One' or 'Section_Two', but returning 'nil' results in an 'NSInternalInconsistencyException':
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeaderCollectionReusableView" forIndexPath:indexPath]; switch (indexPath.section) { case Section_One: return nil; case Section_Two: return nil; case Section_Three: return headerView; case Section_Four: return headerView; case Section_Five: return headerView; default: return nil; } }
What do I need to do to display a header view for only certain sections?
Connecting the Section Header to Data swift in an additional editor pane and Control-drag from the label in the header view over to the file and name the outlet titleLabel. It will add the following code: class FlickrPhotoHeaderView: UICollectionReusableView { @IBOutlet weak var titleLabel: UILabel! }
Overview. Reusable views are so named because the collection view places them on a reuse queue rather than deleting them when they're scrolled out of the visible bounds. Such a view can then be retrieved and repurposed for a different set of content.
Go ahead and return a header for each section and then set the size of the section header to have a size of zero in this UICollectionViewDelegate function.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { return CGSizeZero; }else { return CGSizeMake(self.collectionView.bounds.size.width, desiredHeight); } }
I had a case with one UICollectionViewController
controlling two UICollectionView
s (referenced later as collection view 1 and 2) and I wanted headers to the first and no headers (or footers) to the second.
What's missing from @mwright's answer is that when you return CGSizeZero
for collection view 2 as follows:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { if collectionView == self.collectionView2 { return CGSizeZero } return < something else > }
... means that the collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
does not get called at all for collection view 2.
Meaning that you don't need to worry about returning a "wrong" header for the second collection view in vain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With