Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Header and Footer View

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

like image 213
JayVDiyk Avatar asked Oct 17 '15 05:10

JayVDiyk


People also ask

How do I add a header in collection view?

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 collection reusable view?

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.


2 Answers

UITableView have global headers tableHeaderView and tableFooterView, but UICollectionView does not have global headers so you'll have to use the section headers.

like image 164
Ludovic Landry Avatar answered Sep 17 '22 21:09

Ludovic Landry


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...

like image 42
ikbal Avatar answered Sep 16 '22 21:09

ikbal