ios - 从 View 中的 UITableView 中的 xib 文件加载 UITableViewCell

标签 ios swift uitableview

我有一个 View (不是 View Controller ),当我按下按钮时会弹出。

在此 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 文件:

enter image description here

这里我没有设置 xib 的所有者(我也尝试过),我为 UITableViewCell 添加了自定义类,并且没有重用标识符(我也尝试过)。

在我的 View (包含 TableView 的 View )的 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
}

当我想显示(创建)我的 View 时,调试器停止在

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.'

出了什么问题?

编辑:

enter image description here

最佳答案

检查 @IBOutlet weak var codeLabel: UILabel!PickerViewCell 中 IB 的连接。尝试重新连接。

关于ios - 从 View 中的 UITableView 中的 xib 文件加载 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667050/

相关文章:

ios - 在设备上测试时,PhoneGap 应用程序上未找到 Index.html

ios - NSOperation 属性覆盖 (isExecuting/isFinished)

iOS 8 + 交互式过渡 + 显示的导航栏 = 坏了?

objective-c - 使用 AVAudioPlayer 框架完成播放后,如何再次播放相同的声音?

ios - NSPredicate 在多对多关系上计数 > 0

ios - 在应用程序而不是 Safari 中打开 LPLinkView url

ios - 如何使用 if 条件执行正确的代码方法以显示何时点击 1 到 10

iphone - UITableViewCell 背景图片 - iPad 开发

ios - UITableView 不滚动 (iOS)

ios - UITableViewCell 不保存自定义按钮的状态