I am struggling trying to do multiple sections in my collection view with a header for each section. I don't know Obj-C and I've found a good amount of tutorials for it, but haven't been able to figure out how to convert it into Swift.
All my data is static so all I need is some sort of array or dictionary that I can use to create the multiple sections. I already have a collection view with 1 section working, so if you have any insight or code for multiple sections that'd be helpful.
I know how to set multiple sections using
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return sectionData.count
}
I think the main thing I need help with is implementing this func
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { }
and setting up the data!
UICollectionView and UITableView are almost exactly the same, so if you know how to do multiple sections in a UITableView in Swift, your help is also appreciated
After creating and registering custom header (and/or footers), you can easily specify different header (or footers for that matter) for different section. Here's an example:
Make sure to specify correct number of sections, too:
Worked solution for Swift-3
i)Create Custom Cell && corresponding xib
ii)Register Custom Collection View Cell for HeaderView
iii)Call delegate function to render Custom Header View.
iv)Mention Height of the Custom Header View
The
cellForItemAtIndexPath
function handles populating each section with cells, it does not handle sections or supplementaryViews, and therefore is not the main thing you need help with when it comes to creating section headers.the method you need to implement is
viewForSupplementaryElementOfKind
. Its signature is:Assuming that your collectionView is working correctly for 1 section (you have properly filled out the body of your cellForItemAtIndexPath and your sectionData array properly reflects the number of sections you want to display), you should be able to implement section headers using the following pointers:
Along with cells,
UICollectionView
also supports "supplementary" view objects, typically used for headers or footers. These Supplementary Views act very similarly toUICollectionViewCell
objects. In the same way thatcellForItemAtIndexPath
handles cells, TheviewForSupplementaryElementOfKind
function handles supplementary views.To implement it, you will need to first prepare your ViewController to do so. First edit your layout object to reflect an appropriate header size, that each header will adhere to:
NOTE: I am using a UICollectionViewFlowLayout
Next, if you haven't already done so, create a SectionHeader class that defines each section header object, so you can then register that class with your collectionView object like so:
Here, the first and third argument passed in are the same as a UICollectionViewCell Class registration, the first argument in this method is the reference to the section header class you created. The third is the reuse identifier for the Supplementary View.
The second argument is specific to Supplementary Views, this sets the kind of the SupplementaryView, which in this case is a header, the constant string provided by the UICollectionViewFlowLayout class
UICollectionElementKindSectionHeader
is used for it. If you noticed the parameters on theviewForSupplementaryElementOfKind
, this kind is later passed in as thekind: String
parameter.Fill in the body of your
viewForSupplementaryElementOfKind
the same way you would for a cellForItemAtIndexPath function-- Using thedequeueReusableSupplementaryViewOfKind
method to create a SectionHeader object, then set any attributes as necessary (labels, colors, etc.) and finally return the header object.Hope this helps!!
Reference points:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewDataSource_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDataSource/
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/index.html#//apple_ref/c/data/UICollectionElementKindSectionHeade
@Tarun's answer worked a treat for me; I was missing
collectionView(_:layout:referenceSizeForHeaderInSection:)
, which I needed since sometimes the data to be shown would be sorted and sometimes not.In addition, pinning the section header to the top of the screen (as in the table view Apple's Address Book app) was accomplished by adding the following to
viewDidLoad()
in the UICollectionViewController:Here is the code to achieve UICollection multiple sections made programmatically using SnapKit
ViewController
The View:
The customized section Header
And the cell:
Here is the code that worked for me
create the header cell. To do which i created a custom cell class and a nib to do the customization of the cell in the graphic editor
In viewDidLoad add the following
Then you add the delegate function
This will put the HeaderCell in the SectionView of the PFCollectionView The controls that show in the cell you add them to the xib file as well as the outlets and actions