collectionViewのカスタムセルにボタンを一つ配置しようとコードを書いたのですが、ビルドする前にタイトルのようなエラーが出てしまいます。
どなたか解決法をご教示ください
よろしくおねがいします。
ビューコントローラー;
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupSubviews()
self.autolayoutSubviews()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func setupSubviews() {
let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
flowLayout.minimumInteritemSpacing = 10.0
flowLayout.minimumLineSpacing = 10.0
flowLayout.sectionInset = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
flowLayout.itemSize = CGSizeMake(300.0, 100.0)
self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
self.collectionView!.translatesAutoresizingMaskIntoConstraints = false
self.collectionView!.dataSource = self
self.collectionView!.delegate = self
self.collectionView!.registerClass(ButtonCell, forCellWithReuseIdentifier: "ButtonCell") //ここでエラー
self.collectionView!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(self.collectionView!)
}
func autolayoutSubviews() {
self.collectionView!.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
self.collectionView!.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true
self.collectionView!.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true
self.collectionView!.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ButtonCell", forIndexPath: indexPath) as! ButtonCell
cell.backgroundColor = UIColor.yellowColor()
return cell
}
}
サブクラス;
import UIKit
class ButtonCell: UICollectionViewCell {
var button = UIButton(frame: CGRectMake(0, 0, 250, 50));
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame:CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.whiteColor()
self.setupSubviews()
self.autolayoutSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.whiteColor()
self.setupSubviews()
self.autolayoutSubviews()
}
func setupSubviews() {
button.backgroundColor = UIColor.grayColor();
button.layer.masksToBounds = true;
button.layer.cornerRadius = 20.0;
button.addTarget(self, action: Selector("btn_click:"), forControlEvents:.TouchUpInside);
button.setTitle("Tap Me", forState: UIControlState.Normal);
self.addSubview(button);
}
func autolayoutSubviews() {
}
override func prepareForReuse() {
super.prepareForReuse()
}
}