来自XIB的新自定义单元格导致密钥值符合密钥...?

tom*_*mmi 7 iphone uitableview ios

我有一个使用XIB创建的自定义表格视图单元格:

在此输入图像描述

我还将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];
    }
Run Code Online (Sandbox Code Playgroud)

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

我一直在网上搜索,其中一个可能的原因可能是XIB没有链接到IBOutlet,我已经检查过,似乎并非如此.

Mat*_*oal 32

真正的问题是你如何联系出口.您必须将TableViewCell中的Outlets链接到单元格中的标签(您可能在文件所有者处链接了标签)

这里有一些更明确的图片:

这没关系 在此输入图像描述



这是错的 在此输入图像描述


tom*_*mmi -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];

}
Run Code Online (Sandbox Code Playgroud)