显示节标题UICollectionReusableView

时间:2017-06-22 09:04:43

标签: ios swift xcode uicollectionview uicollectionreusableview

我正在研究iOS应用程序,我在使用UICollectionView单元时遇到了一些问题。

这一次,我想问一下如何显示UICollectionView(UICollectionReusableView)的节标题

我已经实现了如下功能:

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

但是,它总是给出一个空白的结果。请看下面的屏幕截图

Section Header did not show anything (section 1)

section 2

4 个答案:

答案 0 :(得分:3)

您需要返回标题的大小。

 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 
    }

委托方法

  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!
        }

答案 1 :(得分:2)

我为你创建了demo。下载并重复使用到您的代码中。干杯!

下载链接:https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0

答案 2 :(得分:0)

您需要在headerView上添加UILabel

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

答案 3 :(得分:0)

这是我的解决方案。它的完成类似于为 indexPathAt 将单元格/行出列。

集合视图

// 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 子类

class CollectionViewHeader: UICollectionReusableView {
    let identifier = "headerView"
    @IBOutlet weak var titleLabel: UILabel!
}