Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register multiple header supplementary view in the same UICollectionView through storyboards

So I have a UICollectionView that I want to use to display multiple UICollectionViewCells and different headers depending on the state the user is in. The problem is I can't seem to register multiple UICollectionResuableView (header) views within the storyboard. If I try to register one as a footer and use it as a header the app crashes. I also do not see anywhere in the storyboard to declare the number of sections, which would alleviate the problem. For now I'm having to use multiple UICollectionViews and hide/unhide the collectionview I want to display based on the header I need. Is there a better way to do this, or a way to register multiple header views in the same UICollectionView within the storyboard?

like image 912
CA Bearsfan Avatar asked Oct 03 '15 20:10

CA Bearsfan


People also ask

What is the purpose of supplementary views when used with a UICollectionView?

Use Supplementary View to Add Header and Footer in UICollectionView.

How to add header to UICollectionView?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .

What is UICollectionReusableView?

A view that defines the behavior for all cells and supplementary views presented by a collection view.

What is viewForSupplementaryElementOfKind?

collectionView(_:viewForSupplementaryElementOfKind:at:)Asks your data source object to provide a supplementary view to display in the collection view.


1 Answers

You cannot register more than one header in a UICollectionView in a storyboard, but you can register an arbitrary number of them in code. You should create however many headers as you want in nib files (One nib per header, with the header as the top level object), and register them with your collection view instance in viewDidLoad.

You would do this with the registerNib:forSupplementaryViewOfKind:withReuseIdentifier: method.

like image 178
Charles A. Avatar answered Jan 03 '23 23:01

Charles A.