2011-03-17 56 views
4

我收到此错误:错误与自定义的UITableViewCell

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x5a37750> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key destination.' 

以下是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ReservationCell"; 

    ReservationCell *cell = (ReservationCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReservationCell" owner:nil options:nil]; 
     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (ReservationCell *) currentObject; 
       break; 
      } 
     } 
    } 


    //cell.origin.text = [[data objectAtIndex:indexPath.row] origin]; 
    //cell.destination.text = [[data objectAtIndex:indexPath.row] destination]; 
    //cell.time_range.text = [[data objectAtIndex:indexPath.row] time_range]; 

    return cell; 
} 

这里是ReservationCell.h

@interface ReservationCell : UITableViewCell { 
    UILabel * origin; 
    UILabel * destination; 
    UILabel * time_range; 
} 

@property (nonatomic, retain) IBOutlet UILabel * origin; 
@property (nonatomic, retain) IBOutlet UILabel * destination; 
@property (nonatomic, retain) IBOutlet UILabel * time_range; 

@end 

这里是我如何连接它up: enter image description here

+0

你有没有设置单元格的标识符?您通过IB获得了 – Neelesh 2011-03-17 18:11:55

+0

吗?是的,我做到了 – aherlambang 2011-03-17 18:14:14

回答

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

是否将文件所有者设置为零。所以你不能连接你的任何标签。相反,请确保单元的类别为ReservationCell,并且其插座已连接到标签。

+0

是的,我确实改变了...... – aherlambang 2011-03-17 18:04:47

+0

它在这条线上坏了: NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@“ReservationCell”owner:nil options:nil]; – aherlambang 2011-03-17 18:05:19

+0

你是如何连接NIB的UILabel插座的?给细胞? – 2011-03-17 18:08:47

0

当我在故事板中复制了一个原型单元并删除了单元中的一个插口时,我遇到了这个问题。它留下了对插座的参考,但没有连接任何东西。右键单击原型单元格,然后查找其中一个黄色警告标记。

7

此问题发生在CustomCell nib的界面生成器中时,文件的所有者自定义类设置为您的CustomCell类。

  1. 文件的所有者自定义类应始终设置UITableViewCell。
  2. 表视图对象自定义类应该设置为您的CustomCell类。

此外,您需要使用tableview单元格的Nib名称来初始化它。

例(NotificationCell是我的自定义单元格级): http://i42.tinypic.com/29pw0a8.png

10

对于某人来说,谁已经达到了这个线程,而不是能找出像我这样的解决方案,这里是快速的解决这个问题:

在界面构建器中,当您将它们从单元格视图链接起来时,您将文件所有者的IBOutlet链接起来。这就是你得到错误的原因。

祝你好运!!!

+0

非常感谢你这个答案!我从来没有想过自己。 – Rob 2013-05-16 09:38:27

4

我的例外是'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:这个类不是关键值编码兼容的关键字(和我在CustomCell上的名称标签,只有当我得到这个错误时我补上一在CustomCell如标签)

解决问题我的朋友给了我很好的建议添加MyCustomCell.m添加到ProjectSettings - >构建阶段 - >添加MyCustomCell.m文件

1

在我的案例我已将单元的接口添加到头文件,但在.m文件中没有实现。简单地将类的空实现(如下所示)添加到.m文件中可以解决问题。

@implementation myTableViewCell 
@end 

惊讶的是,这还没有列为答案,因为这已经让我多次陷入了困境。希望这可以帮助别人!