Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 此类不符合密钥的键值编码…为什么?_Ios_Uitableview_Swift_Key Value Observing - Fatal编程技术网

Ios 此类不符合密钥的键值编码…为什么?

Ios 此类不符合密钥的键值编码…为什么?,ios,uitableview,swift,key-value-observing,Ios,Uitableview,Swift,Key Value Observing,我已将IB的输出链接到代码,如下所示。 在这里,我正在注册课程: override func viewDidLoad() { self.title = "My Diary" cellNib = UINib(nibName: "TableViewCells", bundle: nil) tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentif

我已将IB的输出链接到代码,如下所示。

在这里,我正在注册课程:

override func viewDidLoad() {
   self.title = "My Diary"
   cellNib = UINib(nibName: "TableViewCells", bundle: nil)
   tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
}

但我一直得到以下运行时错误:

***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“…setValue:forUndefinedKey:::此类不是键值 符合密钥标签的编码要求。'

从以下代码中开始:

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兼容。

有时,当您在xib中创建按钮时,您会创建一个按钮,然后从一个按钮复制粘贴其他按钮,在这种情况下会发生此错误,是的,从xib中删除连接也可能是一个原因。

您不应该在
表视图中调用
实例化所有者
您自己

viewDidLoad
中注册nib,然后
dequeueReusableCellWithIdentifier
将为您完成所有工作


您出现特定错误的原因是,您正在调用
实例化所有者
并将
self
传递为所有者,因此nib试图将插座连接到您的
UITableViewDataSource
实现类,而不是链接到错误源的
DiaryTableViewCell
I

结果如下:


我创建了一个与您的相同的TableViewCell&有相同的问题。我有研究来解决这个问题,但什么都没有。然后我删除TableViewCell中的标签并重新创建,通过文件所有者v..v将其连接到TableViewCell,结果是正确的。没有错误。您应该重新创建它们。

在文档大纲上单击ViewController Right以显示其引用。您可能会在其中一个引用中看到警告。如果仍然需要,请删除它并重新链接


什么是
cellNib?
。为什么不在
viewdiload
中调用
registerClass
,以便
tableView.dequeueReusableCellWithIdentifier
始终返回正确的单元格?您是否已将此“DiaryTableViewCell”类绑定到自定义类下的identity inspector中的TableViewCell?问题可能重复(请参见OP自己的问题).我创建了一个修订的问题,建议更改代码,但仍存在无单元格内容的问题:
cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell