UITableViewCell错误 – 这个类不是关键值编码兼容的关键

当我尝试通过UINib的instantiateWithOwner方法从xib文件加载自定义的UITableViewCell时,出现以下错误。 我尝试了所有其他的解决scheme,我可以find在这里没有运气。 问题似乎是,当UINIB打开xib文件时,它使用超类UITableViewCell而不是我的自定义类ContentPackCell。 我从Interface Builder附加了一个截图,显示了我将xib与我的类关联的位置以及关联了一个标识符。 必须有一些我错过的其他步骤。

错误:

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

代码(类似于Apple的示例AdvancedTableViewCells项目):

 ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { [self.cellNib instantiateWithOwner:self options:nil]; cell = tmpCell; self.tmpCell = nil; } 

在IB中设置自定义类

在IB中设置标识符


更新:

为文件所有者设置类

确保nib中的文件所有者设置为NSObject,并确保没有连接到文件所有者的sockets。

检查你的笔尖中的所有元素,确保它们不引用不再存在的东西。 右键点击它们看什么是指向什么。 那里肯定会有东西。

看起来你已经把你的笔尖中的UILabel与你的代码中不存在的IBOutlet(descriptionLabel)联系起来了。

所以再次检查你的笔尖文件。 我也有几次这个错误,这是我的解决scheme。

我有完全相同的错误,并想知道为什么从.h文件的IBOutlets没有显示时,我右键单击左侧的“对象”中的表格单元格,但只有在文件的所有者。 我发现当我在xib文件的“视图”中左键单击我的自定义表格单元格,然后在属性检查器中将其指定给“identifier”处的正确定制(tableViewCell)类时,出现在表格单元格在“对象”。 也许这有帮助!

在我的情况下,通过检查可编译源列表中是否存在.m来解决问题。

如果您重命名,移动或添加自定义表格单元格(或Interface Builder所需的其他任何东西)的新源,则应将它们添加到Project – > Targets – > – > Build Phases – > Compile Sources。

如果你有多个目标 – 检查所有的目标。 在我的项目中有积极的目标与types的聚合(与自定义IPA构build脚本),我认为这是问题的根源,因为新添加* .m文件错过了应用程序types目标的编译源列表。

 static NSString *CellIdentifier = @"CellIdentifierName"; ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"ContentPackCell" owner:self options:nil] ; cell=objCustomCell; } 

你有没有这样试过

 ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { [self.cellNib instantiateWithOwner:nil options:nil]; //Make owner nil cell = tmpCell; self.tmpCell = nil; } 

通常当你做[[NSBundle mainBundle] loadNibNamed:@"ContentPackCell" owner:nil options:nil]; 你会得到像你现在得到的错误。 所以,如果你得到“这个类不是关键值编码兼容”这样的错误,在这两种情况下,原因是类没有为自定义单元格的“文件所有者”设置。