自定义UITableViewCell出错

ahe*_*ang 4 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.'
Run Code Online (Sandbox Code Playgroud)

以下是代码:

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

这是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
Run Code Online (Sandbox Code Playgroud)

这是我如何连接它: 在此输入图像描述

cod*_*ker 10

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

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

祝好运!!!


Bor*_*dov 7

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

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

您还需要使用tableview单元格的Nib名称对其进行初始化.

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