以自定义标头以编程方式创建UICollectionView [英] Programmatically create UICollectionView with custom headers

查看:81
本文介绍了以自定义标头以编程方式创建UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making an iOS app in swift, and I'm trying to make a collectionView programmatically. I want to use my own subclass of UICollectionReusableView as a header for the CollectionView, because I need some buttons and a stretchable image in the header.

SupView是UICollectionReusableView.

SupView is the 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中插入补充视图,但是在尝试创建标题时出现错误:

I'm trying to insert the Supplementary View in viewForSupplementaryElementOfKind, like this but I'm getting an error when trying to create the header:

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"

The error is in let headerView = ... and says: "signal SIGABRT"

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

也许与

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

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

but if I try to register the SupView-class it gives me error:

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

.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'

有什么想法吗?

请求子类的实现:

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

}

推荐答案

因此,在穆罕默德·法汉德(Mohamad Farhand)的启发下,我找到了答案.

So I figured it out, with inspiration from Mohamad Farhand.

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

The problem was that I had to register the subclass itself with the collectionView, instead of UICollectionReusableView.self, I used the instance of the subclass someView.. So this solved my problem:

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

以及如何初始化视图:

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

这篇关于以自定义标头以编程方式创建UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆