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 answers

0
Eugen Dimboiu 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
Jimmy James 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)
}