【发布时间】:2014-12-31 00:12:31
【问题描述】:
我已将 IB 的输出链接到代码,如下所示。
class DiaryTableViewCell: UITableViewCell {
@IBOutlet weak var TitleLabel: UILabel!
@IBOutlet weak var SubTitleLabel: UILabel!
@IBOutlet weak var leftImageView: UIImageView!
@IBOutlet weak var rightImageView: UIImageView!
}
在这里,我正在注册课程:
override func viewDidLoad() {
self.title = "My Diary"
cellNib = UINib(nibName: "TableViewCells", bundle: nil)
tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
}
但我不断收到以下运行时错误:
*** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“...setValue:forUndefinedKey:]:此类不是键值 键 SubTitleLabel 的编码兼容。'
从以下代码中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as DiaryTableViewCell?
if (cell == nil) {
tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
cell?.selectionStyle = .None
}
if (cell != nil) {
println("\(x++)) Inside cell")
cell!.TitleLabel.text = "Hello"
cell!.SubTitleLabel.text = "World"
}
return cell!
}
具体来说,它发生在这里:
cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
问题:我如何违反 UILabel 的键值编码兼容?
这种情况以前没有发生过... UILabel 是 KVO 兼容的。
【问题讨论】:
-
什么是
cellNib?。为什么不在viewDidLoad中调用registerClass以便tableView.dequeueReusableCellWithIdentifier始终返回正确的单元格? -
您是否在自定义类下的身份检查器中将此“DiaryTableViewCell”类绑定到您的 TableViewCell?
-
问题不可重现(参见 OP 自己的 answer)。
标签: ios uitableview swift key-value-observing