【问题标题】:Programmatically create UICollectionView with custom headers以编程方式创建带有自定义标题的 UICollectionView
【发布时间】:2016-01-25 09:53:00
【问题描述】:

我正在快速制作一个 iOS 应用程序,并且我正在尝试以编程方式制作一个 collectionView。 我想使用我自己的 UICollectionReusableView 子类作为 CollectionView 的标题,因为我需要一些按钮和标题中的可拉伸图像。

SupView 是 UICollectionReusableView。

override func viewDidLoad() {
    super.viewDidLoad()


    let layout = UICollectionViewFlowLayout()
    layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)

    someView = SupView(frame: CGRectMake(0, 0, view.frame.width, 200))

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self

    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")  // UICollectionReusableView
    self.view.addSubview(collectionView)
}

我正在尝试在viewForSupplementaryElementOfKind 中插入补充视图,就像这样,但是在尝试创建标头时出现错误:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    var reusableView : UICollectionReusableView? = nil

    // Create header
    if (kind == UICollectionElementKindSectionHeader) {
        // Create Header
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! SupView
        headerView.frame = CGRectMake(0, 0, view.frame.width, 200)

        reusableView = headerView
    }
    return reusableView!
}

错误在let headerView = ... 中并显示:“signal SIGABRT”

我应该如何初始化headerview,以便我可以输入到我的flowlayout?

也许与

collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")

但如果我尝试注册 SupView 类,它会给我错误:

.../collectionViewPlay/ViewController.swift:32:24: 无法使用类型为“(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)”的参数列表调用“registerClass”

有什么想法吗?

编辑:

请求子类的实现:

import UIKit

class SupView: UICollectionReusableView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.myCustomInit()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.myCustomInit()
    }

    func myCustomInit() {
        print("hello there from SupView")
    }

}

【问题讨论】:

  • 显示SupView的类实现

标签: ios swift uicollectionview subclass


【解决方案1】:

所以我在 Mohamad Farhand 的启发下想通了。

问题是我必须用collectionView注册子类本身,而不是UICollectionReusableView.self,我使用了子类someView的实例。所以这解决了我的问题:

collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")

以及如何初始化视图:

someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView

【讨论】:

    【解决方案2】:

    请注意,Swift 4.1 将 ofKind: 常量重命名为 UICollectionView.elementKindSectionHeader

    【讨论】:

      【解决方案3】:

      你可以这样做:

      // Setup Header
      self.collectionView?.registerClass(CollectionCustomHeader.self, forSupplementaryViewOfKind: CustomeHeaderHeader, withReuseIdentifier: "customHeader")
      

      还有:

      override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
      
      if kind == CustomeHeaderHeader {
          let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "parallaxHeader", forIndexPath: indexPath)
          return view
      }
      

      【讨论】:

      • 我认为您需要稍微加深一下这个答案。就我而言,CollectionCustomHeader.selfCustomeHeaderHeader 是什么?这两个标识符是否应该匹配?
      【解决方案4】:

      这是我在项目中使用的 Swift 3 & 4 答案

      self.collectionView.register(LibraryHeaderNib.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "LibraryHeaderNib")
      

      viewForSupplementaryElementOfKind内部

      let reusableView = self.collectionView!.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "LibraryHeaderNib", for: indexPath) as! LibraryHeaderNib
      
      return reusableView
      

      【讨论】:

        【解决方案5】:

        这是 Swift 5 的答案,我已经在我的项目中使用了它。 在 collectionveiw 中注册类:

        homeDataItemCollection.register(CallDoctorView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: CallDoctorView.identifire)

        然后在集合视图中实现该方法:

        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CallDoctorView.identifire, for: indexPath) as! CallDoctorView return headerView }
        

        【讨论】:

          猜你喜欢
          • 2015-02-04
          • 2013-07-25
          • 1970-01-01
          • 1970-01-01
          • 2014-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多