ios 以编程方式创建带有自定义标题的 UICollectionView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33343367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically create UICollectionView with custom headers
提问by Wiingaard
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.
我正在快速制作一个 iOS 应用程序,我正在尝试以编程方式制作一个 collectionView。我想使用我自己的 UICollectionReusableView 子类作为 CollectionView 的标题,因为我需要一些按钮和标题中的可拉伸图像。
SupView is the UICollectionReusableView.
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)
}
I'm trying to insert the Supplementary View in viewForSupplementaryElementOfKind
, like this but I'm getting an error when trying to create the header:
我正在尝试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!
}
The error is in let headerView = ...
and says: "signal SIGABRT"
错误在let headerView = ...
并说:“信号SIGABRT”
How should I initialize the headerview, so the I can input to my flowlayout?
我应该如何初始化 headerview,以便我可以输入到我的流布局?
maybe somewith with
也许有些
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
but if I try to register the SupView-class it gives me error:
但是如果我尝试注册 SupView 类,它会给我错误:
.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'
.../collectionViewPlay/ViewController.swift:32:24: 无法使用类型为“(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)”的参数列表调用“registerClass”
Any Ideas?
有任何想法吗?
EDIT:
编辑:
The implementation of the subclass was requested:
请求子类的实现:
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")
}
}
采纳答案by Wiingaard
So I figured it out, with inspiration from Mohamad Farhand.
所以我想通了,灵感来自 Mohamad Farhand。
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 注册子类本身,而不是UICollectionReusableView.self
使用子类的实例someView
.. 所以这解决了我的问题:
collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")
And how to initialize the view:
以及如何初始化视图:
someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView
回答by JonJ
Note that Swift 4.1 renames the ofKind:constant as UICollectionView.elementKindSectionHeader
.
请注意,Swift 4.1 将ofKind:常量重命名为UICollectionView.elementKindSectionHeader
.
回答by Mo Farhand
you can do it like this:
你可以这样做:
// Setup Header
self.collectionView?.registerClass(CollectionCustomHeader.self, forSupplementaryViewOfKind: CustomeHeaderHeader, withReuseIdentifier: "customHeader")
also:
还:
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
}
回答by Niall Kiddle
Here is a Swift 3 & 4answer I've used in a project
这是我在项目中使用的Swift 3 & 4答案
self.collectionView.register(LibraryHeaderNib.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "LibraryHeaderNib")
and inside viewForSupplementaryElementOfKind
和里面 viewForSupplementaryElementOfKind
let reusableView = self.collectionView!.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "LibraryHeaderNib", for: indexPath) as! LibraryHeaderNib
return reusableView