Hello I am wondering how do we achieve this?
In UITableView
we could simply do
tableView.tableHeaderView = SomeView;
or
tableView.tableFooterView = SomeView;
But I am wondering how to do the same for UICollectionView
.
P.S.: Not Section Header and Footer
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 .
Reusable views are so named because the collection view places them on a reuse queue rather than deleting them when they are scrolled out of the visible bounds. Such a view can then be retrieved and repurposed for a different set of content.
UITableView
have global headers tableHeaderView
and tableFooterView
, but UICollectionView
does not have global headers so you'll have to use the section headers.
Try it...
First register class in ViewDidLoad
registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")
then use this method
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView
header = SomeView
return header
case UICollectionElementKindSectionFooter:
let footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView
footer= SomeView
return footer
default:
print("anything")
}
}
I hope it help...
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