How to make both header and footer in collection view in swift ?
I'm trying to combine a header and a footer together but it keep crashing, I couldn't find swift tutorial to understand it.
I don't how to return supplementary view for both rather just one .
I set them both on the storyboard (class + identifier )
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { //#warning Incomplete method implementation -- Return the number of sections return 2 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //#warning Incomplete method implementation -- Return the number of items in the section return 10 } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var header: headerCell! var footer: footerCell! if kind == UICollectionElementKindSectionHeader { header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as? headerCell } return header }
Error: UICollectionElementKindCell with identifier one - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> profileCC { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("one", forIndexPath: indexPath) as! profileCC // Configure the cell return cell } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! headerCell headerView.backgroundColor = UIColor.blueColor(); return headerView case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath) as! footerCell footerView.backgroundColor = UIColor.greenColor(); return footerView default: assert(false, "Unexpected element kind") } }
I hope someone will help.
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 .
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.
You can make an UICollectionViewController
to handle the UICollectionView
and in Interface Builder activate the Footer and Header sections, then you can use the following method for preview in you UICollectionView
the two sections added :
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind { case UICollectionView.elementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) headerView.backgroundColor = UIColor.blue return headerView case UICollectionView.elementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath) footerView.backgroundColor = UIColor.green return footerView default: assert(false, "Unexpected element kind") } }
In the above code I put the identifier
for the footer and header as Header
and Footer
for example, you can do it as you want. If you want to create a custom header or footer then you need to create a subclass of UICollectionReusableView
for each and customize it as you want.
You can register your custom footer and header classes in Interface Builder or in code with:
registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
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