尝试注册UICollectionViewCell子类但继续“无法调用'registerClass'与...”

时间:2015-08-21 00:19:10

标签: ios swift syntax compilation

我创建了一个名为UICollectionViewCell的{​​{1}}子类,并在Preview内创建了一个PreviewreuseIdentifier PreviewWindow个单元格故事板编辑器。

我收到错误:

  

../ ViewController.swift:15:29:无法使用类型'的参数列表调用'registerClass'(Preview.Type,forCellWithReuseIdentifier:   字符串)'

UICollectionView

我也尝试了以下变体:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView.registerClass(Preview.self, forCellWithReuseIdentifier: "PreviewWindow")     
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

           let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Preview", forIndexPath: indexPath) as! Preview

        return cell
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
}


class Preview: UICollectionViewCell {

    @IBOutlet weak var bigLabel: UILabel!

}

但得到相同的结果。

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

试试这个:我也使用一些虚拟数据

class Preview: UICollectionViewCell {
@IBOutlet weak var bigLabel: UILabel!
}

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var dummydata = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    dummydata = ["we are young","Swift all day ","IOS all day","SWift better than ObjC"]
      }


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Preview", forIndexPath: indexPath) as! Preview

    cell.bigLabel.text = dummydata[indexPath.row]
    return cell
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.dummydata.count
}
 }
相关问题