swift 以编程方式创建带有自定义标头的UICollectionView

wsewodh2  于 2023-01-25  发布在  Swift
关注(0)|答案(5)|浏览(265)

我正在用swift做一个iOS应用,我尝试用编程的方式做一个collectionView,我想用自己的UICollectionReusableView子类作为CollectionView的头,因为我需要一些按钮和头中的可拉伸图像。
SupView是用户界面集合可重用视图。

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 = ...中,并显示:"信号SIGABRT"

    • 我应该如何初始化标题视图,以便输入到我的流程布局?**

也许有些人

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

但是如果我尝试注册SupView类,它会给我错误:
.../集合视图播放/视图控制器. swift:32:24:无法使用类型为"(SupView!,for SupplementaryViewOfKind:字符串,具有重用标识符:字符串)'
有什么想法吗?

    • 编辑:**

要求实现子类:

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

}
6rqinv9w

6rqinv9w1#

所以我从穆罕默德·法汉德那里得到了灵感。
问题是我必须将子类本身注册到collectionView,而不是UICollectionReusableView.self,我使用子类someView的示例。因此,这解决了我的问题:

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

以及如何初始化视图:

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

v1uwarro2#

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

ngynwnxp

ngynwnxp3#

你可以这样做:

// 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
}
wdebmtf2

wdebmtf24#

以下是我在一个项目中使用的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
pbpqsu0x

pbpqsu0x5#

下面是Swift 5的答案,我已经在我的项目中使用了它。在collectionview中注册类:
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}

相关问题