0

我正在尝试为 UITableview 构建一个自定义单元格。我采取的方法是

  • 子类化UITableViewCell并创建了我自己的自定义类,其中包含 UI 组件(标签、图像等)的成员

  • 创建了一个笔尖并将其类更改为我的自定义类。

这是我班级的代码

头文件

#import <UIKit/UIKit.h>

@interface ActivePoolViewCustomCell : UITableViewCell{
    UILabel* lblDate;
}
@property (nonatomic, retain)IBOutlet UILabel* lblDate;
@end

实施文件

#import "ActivePoolViewCustomCell.h"

@implementation ActivePoolViewCustomCell
@synthesize lblDate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

然后我创建了一个 nib 并将其类更改为 ActivePoolViewCustomCell 并将 UILabel 连接到 lblDate 插座。

现在,我面临的问题是,当我执行以下代码来加载 nib

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"ActivePoolCell";

    ActivePoolViewCustomCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ActivePoolViewCustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = (ActivePoolViewCustomCell*)[topLevelObjects objectAtIndex:0];
        cell.lblDate.text = @"abcd";
    }
    return cell;
}

我收到以下错误

2012-10-30 18:13:56.451 Cda[637:f803] * **由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合键值编码关键 lblDate。

经过一番挣扎,我无法理解这个问题的原因。这里有什么问题 ?还是这个概念本身是错误的?

4

1 回答 1

0

我终于把它整理出来了。问题是,我将 ActivePoolViewCustomCell 类应用于我的自定义单元格笔尖的文件所有者,我应该将其应用于单元格本身。

这个视频帮助我解决了这个问题

http://www.youtube.com/watch?v=d_kO-J3DYvc

于 2012-10-30T14:06:41.610 回答