从XIB创建的新自定义单元格导致“key value coding-compliant for the key ...”错误?

7
我有一个使用XIB创建的自定义表格视图单元格: enter image description here 我已经将XIB文件与我的自定义UITableView单元格链接起来。
但是现在,当我尝试在`- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`中加载单元格时,使用以下代码:
MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:cell options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:1];
    }

我遇到了一个错误: [<NSObject 0x8a5b970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key hitMeButton.

我在网上搜索了一下,可能的原因之一是XIB没有与IBOutlet连接,但我已经检查过了,好像不是这个问题。

2个回答

32

真正的问题在于您如何链接插座(outlets)。 您必须将 TableViewCell 中的插座链接到单元格中的标签(您可能已经将标签链接到文件所有者上了)。

以下是一些更详细的图片说明:

这个是正确的: enter image description here



这个是错误的: enter image description here


我不明白为什么被采纳的答案结果是“-1”,而我的答案没有被采纳,但却获得了18分:P - MatterGoal
在寻找最佳的nsunknownkeyexception线程以发布此解决方案时,我看到了这个,即连接到CELL而不是文件所有者。这只是我的猜测,但你比我先发布了它。 :) - William T. Mallard

-3

我刚刚解决了这个问题,但我不确定那是否是最合适的方式。

if (cell == nil) {

MyCustomTableViewCell *aCell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

 NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:aCell options:nil];

cell = [topLevelObjects objectAtIndex:0];

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接