简体   繁体   English

从视图的UITableView中的xib文件加载UITableViewCell

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

I have a view (not a view controller) that pop up when I press a button. 我有一个按下按钮时会弹出的视图(不是视图控制器)。

In this view, I have a UIButton and a UITableView . 在此视图中,我有一个UIButton和一个UITableView I want to use the UITableViewCell I designed in a xib file. 我想使用我在xib文件中设计的UITableViewCell

So I created my cell class: 因此,我创建了单元格类:

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
    }
}

My xib file: 我的xib文件:

在此处输入图片说明

Here I didn't set the owner of the xib (I tried with as well), I put the custom class for the UITableViewCell and no reuse identifier (I tried with as well). 在这里,我没有设置xib的所有者(我也尝试过),没有放置UITableViewCell的自定义类,也没有重用标识符(我也尝试过)。

In the init function of my view (the one containing the table view), I do the following: 在视图的init函数(包含表视图的init函数)中,执行以下操作:

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

And finally I implement the method: 最后,我实现了该方法:

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
}

When I want to show (create) my view, the debugger stop at 当我想显示(创建)我的视图时,调试器在以下位置停止

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

and I get the following error: 我收到以下错误:

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

What's wrong? 怎么了?

EDIT: 编辑:

在此处输入图片说明

Check your connection of @IBOutlet weak var codeLabel: UILabel! 检查@IBOutlet weak var codeLabel: UILabel!连接@IBOutlet weak var codeLabel: UILabel! with IB in PickerViewCell . 与IB在PickerViewCell Try to reconnect it. 尝试重新连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM