【问题标题】:add custom header to collection view swift将自定义标题添加到集合视图 swift
【发布时间】:2017-02-13 08:21:34
【问题描述】:

我正在尝试使用自定义 xib 文件将标题添加到 collectionView。我用实现UICollectionReusableView 的类创建了xib 文件。 在collectionViewController 我注册了xib 文件,如下所示:

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。 任何缺少的代码?

HCollectionReusableView 类:

class HCollectionReusableView: UICollectionReusableView {

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

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



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

}

【问题讨论】:

    标签: swift uicollectionview uicollectionreusableview


    【解决方案1】:

    您需要像这样拨打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")
        }
    }
    

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

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

    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:

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

    Swift 4.1 更新

    UICollectionElementKindSectionHeader 已重命名为 UICollectionView.elementKindSectionHeader

    【讨论】:

    • 我想我必须将 xib 的注册更改为类,这里现在出现错误:无法将一种视图出列:带有标识符 HCollectionReusableView 的 UICollectionElementKindSectionHeader - 必须为标识符注册一个 nib 或一个类或连接情节提要中的原型单元(空)
    • 您收到错误,因为当您将项目和注册时的标识符不匹配...尝试在注册单元格时像这样对标识符进行硬编码:self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView") 这应该可以...然后您可以调整代码以满足您的需求
    • 只有在完全不使用 Xib 或 Storyboard 的情况下才会注册课程
    • 太棒了,拯救我的一天
    • 你仍然需要注册你的类,而不是 nib,否则它不会进行类型转换。 yourCollectionView.register(YourClassName.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "YourHardCodeIdentifierString")
    【解决方案2】:
    final class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.collectionView!.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MyCollectionReusableView.reuseIdentifierHeader)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 40)
    }
    
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MyCollectionReusableView.reuseIdentifierHeader, for: indexPath) as! MyCollectionReusableView
        header.setupView()
        
        return header
    }
    }
    
    final class MyCollectionReusableView: UICollectionReusableView {
    
       static let reuseIdentifierHeader = "MyId"
    
       func setupView() {
          //Code...
       }
    
    }
    

    【讨论】:

      【解决方案3】:

      您是否在 xib 文件中设置了文件的所有者设置?将文件所有者更改为托管 collectionView 的视图控制器。

      【讨论】:

      • 是的,我做到了,在文件所有者和集合可重用视图中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多