iOSアプリケーションを迅速に作成しています。コレクションビューをプログラムで作成しようとしています。 UICollectionReusableViewの独自のサブクラスをcollectionviewのヘッダーとして使用したいのですが、ヘッダーにはいくつかのボタンと伸縮可能なイメージが必要です。iOS;プログラムでコレクションを作成するカスタムヘッダーで表示する
SupViewはUICollectionReusableViewです。私はこのように、viewForSupplementaryElementOfKind
に補足ビューを挿入しようとしているが、私は、ヘッダーを作成しようと、エラーを取得しています
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)
}
:
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"
私はflowlayoutに入力できるように、ヘッダービューをどのように初期化する必要がありますか?は多分
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
でsomewithが、私はSupViewクラスを登録しようとした場合、それは私にエラーを与える:
.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'
任意のアイデア?
EDIT:サブクラスの実装が要求された
:
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のクラス実装を表示 – Shoaib