以自定义标头以编程方式创建UICollectionView

编程入门 行业动态 更新时间:2024-10-25 08:20:01
本文介绍了以自定义标头以编程方式创建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

本文发布于:2023-11-28 07:34:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   方式   UICollectionView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!