7

I have added the delegate function in the controller class but it won't called at the run time.

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Robin JR
  • 141
  • 1
  • 2
  • 11

4 Answers4

9
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! UICollectionReusableView

        headerView.backgroundColor = UIColor.blueColor();
        return headerView

    case UICollectionElementKindSectionFooter:
        let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView

        footerView.backgroundColor = UIColor.greenColor();
        return footerView

    default:

        assert(false, "Unexpected element kind")
    }
}
Nikunj5294
  • 391
  • 3
  • 14
  • 1
    this delegate didnt called at run time – Robin JR Dec 15 '15 at 07:06
  • 1
    I'm calling this: (Swift 3) `collectionView?.register(HeaderCollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell") ` I'm still not able to call the delegate! – rn3sto Oct 09 '16 at 08:52
7

I follow this tutorial Appcoda tutorial and update code For swift 4.0

Register the class :

self.collectionView.register(HeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeadView")

Delegates :

// For header size (required)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width:collectionView.frame.size.width, height:50.0)
}


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeadView", for: indexPath)

        headerView.backgroundColor = UIColor.blue;
        return headerView

    default:

        fatalError("Unexpected element kind")
    }
}
vp2698
  • 1,783
  • 26
  • 28
3

Delegate method not called because you need to register a class for the supplementary view;

yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionHeaderID")

yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "collectionFooterID")
Kemal Can Kaynak
  • 1,638
  • 14
  • 26
2

If you use Custom UICollectionViewFlowLayout

set headerReferenceSize

let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .vertical
    flowLayout.minimumLineSpacing = 0
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.sectionHeadersPinToVisibleBounds = true
    flowLayout.headerReferenceSize = CGSize(width:      self.collectionView.frame.size.width, height: 50)
Marosdee Uma
  • 719
  • 10
  • 14