Wii*_*ard 18 subclass ios uicollectionview swift
我在swift中制作一个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)
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试插入补充视图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!
}
Run Code Online (Sandbox Code Playgroud)
错误在于let headerView = ...并说:"信号SIGABRT"
我应该如何初始化headerview,以便我可以输入到我的flowlayout?也许有些人
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
Run Code Online (Sandbox Code Playgroud)
但如果我尝试注册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")
}
}
Run Code Online (Sandbox Code Playgroud)
Wii*_*ard 19
所以我从Mohamad Farhand的灵感中找到了答案.
问题是我必须使用collectionView注册子类本身,而不是UICollectionReusableView.self,我使用了子类的实例someView.所以这解决了我的问题:
collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")
Run Code Online (Sandbox Code Playgroud)
以及如何初始化视图:
someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31355 次 |
| 最近记录: |