自定义UITableViewCell子类:此类不是符合编码规范的键值

ace*_*des 9 iphone xcode objective-c uitableview

我在stackoverflow中看了几乎所有关于这个的相关问题,并尝试了所有可能的解决方案,但我仍然无法弄清楚为什么我会收到此错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CustomCell 0x6e627d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key title.'
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用nibname加载自定义表格视图单元格:@"CustomCell".它的类被分配给"CustomCell"(与nib名称相同的类名).它的文件所有者也设置为加载这些单元格的类 - "ProductsTableViewControllerWithSearch".笔尖中的所有插座都连接到CustomCell.m中的插座

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellClassName = @"CustomCell";
    static NSString *CellIdentifier = @"Custom Items Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {

      NSArray *topLevelItems =  [[UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];

    }
//... more irrelevant code here
}
Run Code Online (Sandbox Code Playgroud)

请有人帮帮我.我已经工作了4个多小时.非常感谢!

PS:我正在使用ARC,正在为iOS 5开发.

这是我的CustomCell.h.

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell{
    UILabel *textLabel;
    UILabel *detailTextLabel;
    UIButton *starButton;
}

@property (strong, nonatomic) IBOutlet UILabel *textLabel;
@property (strong, nonatomic) IBOutlet UILabel *detailTextLabel;
@property (strong, nonatomic) IBOutlet UIButton *starButton;

@end
Run Code Online (Sandbox Code Playgroud)

我的CustomCell.m中没有任何内容

小智 13

如果要链接任何不属于该视图的内容,则会发生此错误.

在这种情况下,解决方案是在Interface Builder中

  1. 首先将文件的所有者标识类更改为NSObject.
  2. 取消链接文件所有者中的每个IBOutlet.
  3. 将自定义NIB的标识类更改为CustomTableViewCell类的名称.
  4. 在此处链接您的所有IBOutlet.

而已.

  • 您的CustomTablecell的UILabel应该连接到UITableViewCell对象,而不是File的所有者.我每次都忘记这一点.我不知道为什么. (9认同)

Joe*_*asq 9

在Interface Builder中,您需要将单元格的类设置为CustomCell,而CustomCell需要具有属性title.

this class is not key value coding-compliant for the key …

通常意味着在类上找不到属性,Interface Builder正在尝试使用该属性,从而导致崩溃.


QED*_*QED 5

我假设您在运行时从NIB加载单元格时出现此错误.(如果相反它会打破编写的某些代码行,为了上帝的缘故,删除那行代码!)

进入您的自定义单元格的XIB视图,按住Ctrl键单击"文件所有者",然后取消链接您在那里所谓的"标题"连接.如果你感觉很精神,取消所有内容的链接,那么重新链接你想要的东西.看看是否清除它.如果不是,我会在这里待几个小时.

  • 添加`-setTitle:`方法和log/breakpoint标题设置的内容. (2认同)