How to get header of collection view in swift?

6.6k Views Asked by At

How to get Header of collection view on DidSelectItemAtIndexPath for change text of header while select item.

2

There are 2 best solutions below

0
On

As of iOS 9, you can use:

func supplementaryView(forElementKind elementKind: String, 
                 at indexPath: IndexPath) -> UICollectionReusableView? 

to get a supplementary view by index path.

Check out Apple's documentation

8
On

You can use:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    let indexHeaderForSection = NSIndexPath(row: 0, section: indexPath.section) // Get the indexPath of your header for your selected cell
    let header = collectionView.viewForSupplementaryElementOfKind(indexHeaderForSection)
}