Display Section Header UICollectionReusableView

别来无恙 提交于 2019-12-23 02:22:33

问题


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


回答1:


You need to return size of header .

 func collectionView(_ collectionView: UICollectionView,
                     layout collectionViewLayout: UICollectionViewLayout,
                     referenceSizeForHeaderInSection section: Int) -> CGSize{
       return CGSize(width: CGFloat(collectionView.frame.size.width, height: CGFloat(135)) // you can change 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) // cellHea 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!
        }



回答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




回答3:


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")
    }
}


来源:https://stackoverflow.com/questions/44694923/display-section-header-uicollectionreusableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!