简体   繁体   English

从 xib 加载 UITableViewCell(该类不符合键值编码)

[英]Loading UITableViewCell from a xib (this class is not key value coding-compliant for the key)

I'm curious as to the proper way to use a xib file to layout the contents of a UITableViewCell.我很好奇使用 xib 文件来布局 UITableViewCell 内容的正确方法。 When I try to follow all the steps I find on the Internet I always get当我尝试按照在 Internet 上找到的所有步骤进行操作时,我总是得到

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x10fe7d790> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key statusLabel.'由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<NSObject 0x10fe7d790> setValue:forUndefinedKey:]: 此类与键 statusLabel 的键值编码不兼容。”

So here's the related code所以这是相关的代码

@interface MyCell : UITableViewCell

@property (nonatomic,strong) IBOutlet UILabel* messageLabel;
@property (nonatomic,strong) IBOutlet UILabel* statusLabel;

And in my UIViewController I've tried both在我的 UIViewController 中,我都尝试过

-(void)viewDidLoad {
     [self.tableView registerNib:[UINib nibWithNibName:@"MyCell"
                                           bundle:[NSBundle mainBundle]]
     forCellReuseIdentifier:@"CustomCellReuseID"];
}

or using或使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomCellReuseID";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( !cell ) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil] 
lastObject];
        // or sometimes owner is nil e.g.
        //cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil] 
lastObject];
    }

    // ... remainder of cell setup

    return cell;
}

Both of these approaches fail with the exception I mentioned in the title.除了我在标题中提到的例外,这两种方法都失败了。 It appears that with owner:self the error is because the UIViewController doesn't have the IBOutlet properties.看来,与 owner:self 错误是因为 UIViewController 没有 IBOutlet 属性。 With owner:nil it's because it internally uses an NSObject which of course also doesn't have the IBOutlet properties.使用 owner:nil 是因为它在内部使用了一个 NSObject,当然它也没有 IBOutlet 属性。

The only work around I've found is as follows.我发现的唯一解决方法如下。 Within the init method for my cell I add the returned view/cell to my initializing cell在我的单元格的 init 方法中,我将返回的视图/单元格添加到我的初始化单元格

eg例如

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // nil out properties

        [self.contentView addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil] lastObject]];
    }
    return self;
}

This seems really hokey (although I can also just change the base type in the xib to UIView instead of MyCell or UITableViewCell) which makes it feel slightly less hokey.这看起来真的很奇怪(虽然我也可以将 xib 中的基本类型更改为 UIView 而不是 MyCell 或 UITableViewCell),这让它感觉不那么矫情。

I've seen a lot of posts where people run into this particular error.我看过很多帖子,人们遇到了这个特定的错误。 This is usually explained away as being a wiring problem in the xib itself, however if I remove all connections in the xib it loads fine, but the moment I add back a connection between a ui element and the file owner the error returns, so I don't think it has anything to do with 'cleaning up' the xib (even looking at the XML for the xib there's no errant connections listed).这通常被解释为是 xib 本身的接线问题,但是如果我删除 xib 中的所有连接,它会加载正常,但是当我添加回 ui 元素和文件所有者之间的连接时,错误返回,所以我不要认为它与“清理”xib 没有任何关系(即使查看 xib 的 XML,也没有列出错误的连接)。

Does anyone else have any thoughts as to how this error comes about?有没有其他人对这个错误是如何产生的有任何想法?

Have you connected the outlets for "messageLabel and statusLabel" in the Cell Nib file?您是否已连接 Cell Nib 文件中“messageLabel 和 statusLabel”的插座? The error states that the IBOutlet property for "statusLabel" is not found in the Nib file (connection issue).该错误指出在 Nib 文件中找不到“statusLabel”的 IBOutlet 属性(连接问题)。

I had to make sure that when creating the outlet to specify that I was hooking to the cell, not the object's owner.我必须确保在创建插座时指定我正在连接到单元格,而不是对象的所有者。 When you drag your connection from the label in your cell to the class, the menu appears so you can name it and you have to select it in the 'object' dropdown menu (you can choose 'files owner' or the cell's class name, choose the cell's class name).当您将连接从单元格中的标签拖到类时,会出现菜单,以便您可以命名它,您必须在“对象”下拉菜单中选择它(您可以选择“文件所有者”或单元格的类名,选择单元格的类名)。 Of course you must declare the cell's class as this class too, not just 'TableViewCell'.当然,您也必须将单元格的类声明为此类,而不仅仅是“TableViewCell”。 Otherwise I would keep getting the class not key compliant.否则我会继续让课程不符合关键要求。 So now I have both the cell and the files owner named of that class.所以现在我拥有该类的单元格和文件所有者。

Also Check If MyCell.xib file is not (mistakenly!) get added inside还要检查 MyCell.xib 文件是否(错误地!)被添加到里面

Target Settings -> Build Phases -> Compile Sources

Compile Sources are For All .m Files and Not for .Xib resource files.编译源适用于所有 .m 文件而不适用于 .Xib 资源文件。

And并且

Copy Bundle Resources are for .Xib resource files.复制捆绑资源用于 .Xib 资源文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM