Ios 以编程方式创建带有自定义标题的UICollectionView

Ios 以编程方式创建带有自定义标题的UICollectionView,ios,swift,uicollectionview,subclass,Ios,Swift,Uicollectionview,Subclass,我正在用swift制作一个iOS应用程序,并试图以编程方式制作一个collectionView。 我想使用我自己的UICollectionReusableView子类作为CollectionView的标题,因为我需要一些按钮和标题中的可伸缩图像 SupView是UICollectionReusableView override func viewDidLoad() { super.viewDidLoad() let layout = UICollectionViewFlowL

我正在用swift制作一个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)
}
我正试图在
视图中插入补充视图,以供SupplementalElementOfKind
,如下所示,但在尝试创建标题时出错:

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=…
中,并显示:“信号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")
    }

}

您可以这样做:

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


所以我从穆罕默德·法尔汉德那里得到了灵感

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

collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")
以及如何初始化视图:

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

下面是我在一个项目中使用的一个swift3&4答案

self.collectionView.register(LibraryHeaderNib.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "LibraryHeaderNib")
以及种类的辅助元素的内部
视图

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

return reusableView

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

显示SupView的类实现。我认为您需要加深回答。在我的例子中,什么是
CollectionCustomHeader.self
CustomeHeader
?假设这两个标识符匹配吗?
let reusableView = self.collectionView!.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "LibraryHeaderNib", for: indexPath) as! LibraryHeaderNib

return reusableView