【发布时间】:2016-01-25 09:53:00
【问题描述】:
我正在快速制作一个 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 = ... 中并显示:“signal 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")
}
}
【问题讨论】:
-
显示SupView的类实现
标签: ios swift uicollectionview subclass