UITableViewCell错误 - 此类不是密钥值编码兼容的密钥

时间:2012-08-31 12:09:20

标签: iphone objective-c ios cocoa-touch cocoa-bindings

当我尝试通过UINib的instantiateWithOwner方法从xib文件加载自定义UITableViewCell时,我收到以下错误。我已经尝试了所有其他解决方案,我可以在这里找到没有运气。问题似乎是当Uibib打开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;
}

Setting the custom class in IB

Setting an Identifier in IB


更新

Setting the class for File's Owner

7 个答案:

答案 0 :(得分:36)

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

答案 1 :(得分:5)

检查笔尖中的所有元素,确保它们不引用不再存在的内容。右键单击它们以查看指向什么的内容。肯定会有一些东西。

答案 2 :(得分:2)

看起来您已将笔尖中的UILabel与您的代码中不再存在的IBOutlet链接(descriptionLabel)。

请再次检查您的nib文件。我也有过几次这个错误,这对我来说就是解决方案。

答案 3 :(得分:1)

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

答案 4 :(得分:1)

在我的案例中,通过在可编译源列表中检查.m的存在来解决问题。

如果您重命名,移动或添加自定义表格单元格的新来源(或Interface Builder所需的任何其他内容),您应将它们添加到Project - &gt;目标 - &gt; - &GT;构建阶段 - &gt;编译来源。

如果您有多个目标 - 请检查所有目标。在我的项目中,存在具有Aggregate类型的活动目标(具有自定义IPA构建脚本),并且我认为这是问题的根源,因为新添加的* .m文件错过了应用程序类型目标的编译源列表。

答案 5 :(得分:0)

static NSString *CellIdentifier = @"CellIdentifierName";
ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ContentPackCell" owner:self options:nil] ;
    cell=objCustomCell;
}

答案 6 :(得分:0)

你有没有试过这个,

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];时,您会收到类似于现在的错误。因此,如果您收到类似“此类不符合键值编码”的错误,则在这两种情况下,原因是“自定义”单元格的“文件所有者”未设置“类”。