[英]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.