Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow A Header View for Only Certain Sections Using an iOS UICollectionView

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?

like image 766
Gifreakius Avatar asked May 28 '14 00:05

Gifreakius


People also ask

How to add section header in CollectionView Swift?

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! }

What is collection reusable view?

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.


2 Answers

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);     } } 
like image 73
mwright Avatar answered Sep 17 '22 08:09

mwright


I had a case with one UICollectionViewController controlling two UICollectionViews (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.

like image 35
Markus Rautopuro Avatar answered Sep 20 '22 08:09

Markus Rautopuro