iOS版;以编程方式uicollectionView与自定义标头

问题描述 投票:14回答:4

我在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)
}

我正在尝试在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”

我应该如何初始化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")
    }

}
ios swift uicollectionview subclass
4个回答
16
投票

所以我从Mohamad Farhand的灵感中找到了答案。

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

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

以及如何初始化视图:

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

5
投票

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


1
投票

你可以这样做:

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

0
投票

这是我在项目中使用的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
© www.soinside.com 2019 - 2024. All rights reserved.