首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义UITableViewCell出错

自定义UITableViewCell出错
EN

Stack Overflow用户
提问于 2011-03-18 01:57:20
回答 6查看 4K关注 0票数 4

我得到了这个错误:

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

代码如下:

代码语言:javascript
复制
- (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

代码语言:javascript
复制
@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

下面是我如何把它连接起来的:

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-03-18 02:02:19

代码语言:javascript
复制
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReservationCell"
                                                         owner:nil 
                                                       options:nil];

正在将文件所有者设置为nil。所以你不能把你的任何标签都连接到上面。相反,请确保单元格的类是ReservationCell,并且其插座连接到标签。

票数 4
EN

Stack Overflow用户

发布于 2012-05-14 19:07:44

对于像我这样已经接触到这个主题但不能找到解决方案的人,这里是这个问题的快速解决方案:

在界面构建器中,当您应该从单元格视图中链接IBOutlets时,您却链接了来自文件所有者的文件。这就是你得到这些错误的原因。

祝你好运!

票数 10
EN

Stack Overflow用户

发布于 2012-03-21 21:39:57

当在File的接口构建器中,将CustomCell的Owner自定义类设置为您的CustomCell类时,就会发生此问题。

应始终将视图文件的所有者自定义类设置为您的CustomCell类。

此外,您还需要使用您的表视图单元格的Nib名称初始化它。

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5343172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档