iOS版;以自定义标头编程的collectionView [英] iOS; programmatically collectionView with custom headers
问题描述
我正在使用swift创建一个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 strechable 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!
}
错误在让headerView = ...
并说:信号SIGABRT
The error is in let headerView = ...
and says: "signal SIGABRT"
我应该如何初始化标题视图,以便我可以输入到我的flowlayout?也许有一些
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
但是如果我尝试注册SupView-class它会给我错误:
but if I try to register the SupView-class it gives me error:
... / collectionViewPlay / ViewController.swift:32:24:无法使用类型'的参数列表调用'registerClass'(SupView!,forSupplementaryViewOfKind:String,withReuseIdentifier:String )'
.../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 it selves with the collectionView, instead of UICollectionReusableView.self
or 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
这篇关于iOS版;以自定义标头编程的collectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!