自定义UITableViewCell出错

时间:2011-03-17 17:57:20

标签: iphone objective-c uitableview

我收到此错误:

 *** 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

以下是我如何连线: enter image description here

6 个答案:

答案 0 :(得分:10)

对于那些已经达到此主题并且无法找到像我这样的解决方案的人来说,这是解决此问题的快速解决方案:

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

祝你好运!!!

答案 1 :(得分:7)

在CustomCell nib的界面构建器中,将File's Owner自定义类设置为CustomCell类时会发生此问题。

  1. 文件的所有者自定义类应始终设置为UITableViewCell。
  2. 应将表视图对象自定义类设置为CustomCell类。
  3. 此外,您还需要使用tableview单元格的Nib名称对其进行初始化。

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

答案 2 :(得分:4)

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

将文件所有者设置为nil。因此,您无法将任何标签连接到该标签。相反,请确保单元格的类别为ReservationCell,并且其出口连接到标签。

答案 3 :(得分:4)

我的异常是'NSUnknownKeyException',原因是:'[setValue:forUndefinedKey:]:这个类不是密钥值编码兼容的密钥(和我在CustomCell上的名称标签,我只有在我遇到此错误在CustomCell中添加了一些内容,例如标签)

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

答案 4 :(得分:1)

在我的情况下,我已将单元格的接口添加到头文件中,但在.m文件中没有实现。只需将类的空实现(如下所示)添加到.m文件即可解决问题。

@implementation myTableViewCell
@end

感到惊讶的是,这并没有被列为答案,因为这在过去曾多次困扰过我。希望这有助于某人!

答案 5 :(得分:0)

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

相关问题