簡體   English   中英

從視圖的UITableView中的xib文件加載UITableViewCell

[英]Load UITableViewCell from xib file in a UITableView in a view

我有一個按下按鈕時會彈出的視圖(不是視圖控制器)。

在此視圖中,我有一個UIButton和一個UITableView 我想使用我在xib文件中設計的UITableViewCell

因此,我創建了單元格類:

class PickerViewCell: UITableViewCell {

    @IBOutlet weak var flagImageView: UIImageView!
    @IBOutlet weak var codeLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!

    func setCell(myObject: myObject) {
        flagImageView.image = UIImage(named: myObject.code)
        codeLabel.text = myObject.code
        codeLabel.font = myObject.kPickerViewCodeFont
        nameLabel.text = myObject.name
        nameLabel.font = myObject.kPickerViewNameFont
    }
}

我的xib文件:

在此處輸入圖片說明

在這里,我沒有設置xib的所有者(我也嘗試過),沒有放置UITableViewCell的自定義類,也沒有重用標識符(我也嘗試過)。

在視圖的init函數(包含表視圖的init函數)中,執行以下操作:

let nib = UINib(nibName: "PickerViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")

最后,我實現了該方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell

    let myObject = myObjects[indexPath.row]
    cell.setCell(myObject)

    return cell
}

當我想顯示(創建)我的視圖時,調試器在以下位置停止

let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell

我收到以下錯誤:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7f9b934d8c70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key codeLabel.'

怎么了?

編輯:

在此處輸入圖片說明

檢查@IBOutlet weak var codeLabel: UILabel!連接@IBOutlet weak var codeLabel: UILabel! 與IB在PickerViewCell 嘗試重新連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM