27127


collectionViewのカスタムセルにボタンを一つ配置しようとコードを書いたのですが、ビルドする前にタイトルのようなエラーが出てしまいます。  
どなたか解決法をご教示ください  
よろしくおねがいします。

ビューコントローラー；

[code]

    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
    
        }
    }
    
[/code]

サブクラス；

[code]

    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()
    
        }
    }
    
[/code]


CC BY-SA 3.0
2016-06-28T03:10:45.087
27125
2016-10-26T01:38:13.360
2016-10-26T01:38:13.360
7256
16775
question
swift
error: Expected member name or constructor call after type name


`registerClass(_: forCellWithReuseIdentifier:)`の第一引数に渡すものは、`AnyClass?`です。

[code]

    self.collectionView!.registerClass(ButtonCell.self, forCellWithReuseIdentifier: "ButtonCell")
    
[/code]

クラスを値として渡すには、クラス名の後に`.self`を使います。

Swift の Language Reference の [Types - Metatype
Type](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html)
の項を参照してみてください。


CC BY-SA 3.0
2016-06-28T05:03:23.317
27127
2016-06-28T05:03:23.317
5337
27125
answer