ios – 使用故事板和子类自定义UITableViewCell

我创建了一个UITableViewCell的子类,我称之为DCCustomCell.这是DCCustomCell.h文件.
#import <UIKit/UIKit.h>

@interface DCCustomCell : UITableViewCell

@property (weak,nonatomic) IBOutlet UIImageView *imageView;
@property (weak,nonatomic) IBOutlet UILabel *title;
@property (weak,nonatomic) IBOutlet UILabel *date;
@property (weak,nonatomic) IBOutlet UILabel *price;

@end

所有礼节都与我的iPhone.storyboard文件中的元素正确连接.
我还在iPhone.storyboard中选择了tableViewCell,并将我的单元格标识符设置为“CustomCell”,将类设置为DCCustomCell.

这是UITableViewController的viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"];

    // Do any additional setup after loading the view,typically from a nib.
}

这是tableView:cellForRowAtIndexPath:方法:

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

    DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

    NSLog(@"%@",cell);

    cell.imageView.image = [UIImage imageNamed:@"info_button.png"];
    cell.title.text = @"Hello!";
    cell.date.text = @"21/12/2013";
    cell.price.text = @"€15.00";

    return cell;
}

问题是imageView已正确设置,但所有标签都是空白的.如果我将imageView属性名称更改为例如eventImageView,则不会设置图像.
这是结果:

我无法弄清楚如何解决这个问题.

编辑:如果我删除

[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"];

从viewDidLoad似乎都工作.为什么?

解决方法

正如您所注意到的那样,当您移除时
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"];

如果使用dequeueReusableCellWithIdentifier,则只需执行此操作:indexPath:AND尚未在该单元的标识检查器中设置自定义类.如果使用registerClass:forCellReuseIdentifier,则将使用initWithStyle初始化单元格:reuseIdentifier:而不是initWithCoder:从而搞砸了您在故事板中所做的连接.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...