2

I was working on iOS application and I have several problem about using UICollectionView cell.

This time, I want to ask about how to display the section header of UICollectionView (UICollectionReusableView)

I already implement the function like below :

public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:

            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
            var labelHeader = headerView.viewWithTag(2) as! UILabel

            if indexPath.section == 0 {
                labelHeader.text = "Specialist Clinic"

            }
            else {
                labelHeader.text = "Medical Support"
            }

            headerView.backgroundColor = UIColor.blue;
            return headerView

        default:
            assert(false, "Unexpected element kind")
        }
    }

but, it always give a blank result. please look at the screen shot below

Section Header did not show anything (section 1)

section 2

christ2702
  • 473
  • 6
  • 12
  • 26

4 Answers4

6

You need to return size of header.

func collectionView(_ collectionView: UICollectionView,
                     layout collectionViewLayout: UICollectionViewLayout,
                     referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: 123) // you can change sizing here 
}

Delegate method

  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            var reusableview: UICollectionReusableView? = nil
            if kind == UICollectionElementKindSectionHeader {
                reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath) // cellHeader is your identifier
            var labelHeader = reusableview.viewWithTag(2) as! UILabel

         if indexPath.section == 0 {
                labelHeader.text = "Specialist Clinic"

            }
            else {
                labelHeader.text = "Medical Support"
            }

            headerView.backgroundColor = UIColor.blue;
               
            }
            return reusableview!
        }
Deitsch
  • 1,610
  • 14
  • 28
KKRocks
  • 8,222
  • 1
  • 18
  • 84
2

I have created demo for you. Download and re-use into your code. Cheers!

Download Link : https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0

Parimal
  • 277
  • 2
  • 8
0

You need to add UILabel on headerView

      public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
        var labelHeader = headerView.viewWithTag(2) as! UILabel

        if indexPath.section == 0 {
            labelHeader.text = "Specialist Clinic"

        }
        else {
            labelHeader.text = "Medical Support"
        }

        headerView.backgroundColor = UIColor.blue;
        headerView.addSubview(labelHeader) //Add UILabel on HeaderView
        return headerView

    default:
        assert(false, "Unexpected element kind")
    }
}
0

Here is my solution. It's done similar to dequeueing a cell/row for indexPathAt.

CollectionView

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

// content of header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    guard let headerCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CollectionViewHeader.identifier, for: indexPath) as? CollectionViewHeader else {
        return UICollectionReusableView()
    }
    headerCell.titleLabel.text = "Title"
    return headerCell
}

UICollectionReusableView subclass

class CollectionViewHeader: UICollectionReusableView {
    let identifier = "headerView"
    @IBOutlet weak var titleLabel: UILabel!
}
Deitsch
  • 1,610
  • 14
  • 28