将自定义标题添加到集合视图swift [英] add custom header to collection view swift

查看:114
本文介绍了将自定义标题添加到集合视图swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义xib文件将标头添加到collectionView.我使用实现UICollectionReusableView的类创建了xib文件. 在collectionViewController中,我这样注册了xib文件:

I am trying to add header to collectionView using custom xib file. I created the xib file with class implementing UICollectionReusableView. In collectionViewController I registered the xib file like this:

self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)

然后在viewForSupplementaryElementOfKind中我做了

let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier, for: indexPath) as! HCollectionReusableView

和调整大小

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: 100, height: 50)
}

我收到错误消息:无法将NIB捆绑装入. 任何缺少的代码?

I am getting error: Could not load NIB in bundle. any missing code ?

HCollectionReusableView类:

HCollectionReusableView class:

class HCollectionReusableView: UICollectionReusableView {

static var nibName : String
    {
    get { return "headerNIB"}
}

static var reuseIdentifier: String
    {
    get { return "headerCell"}
}



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

推荐答案

您需要像这样调用viewForSupplementaryElementOfKind:

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

    switch kind {
    case UICollectionElementKindSectionHeader:
           let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView

            reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
         //do other header related calls or settups
            return reusableview


    default:  fatalError("Unexpected element kind")
    }
}

这样,您可以初始化并显示标题

This way you can initialise and show the header

设置UICollectionViewHeader框架的另一种方法是通过扩展UICollectionViewDelegateFlowLayout这样:

Another way of setting the UICollectionViewHeader frame is by extending UICollectionViewDelegateFlowLayout like this:

extension UIViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 100) //add your height here
    }
}

这消除了呼叫的必要:

reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)

在上面提到的

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

通过调用以下内容初始化UICollectionView后,请记住注册HeaderView:

Remember to register the HeaderView after you initialise your UICollectionView by calling:

collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")

Swift 4.1更新

UICollectionElementKindSectionHeader已重命名为UICollectionView.elementKindSectionHeader

这篇关于将自定义标题添加到集合视图swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆