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:从而搞砸了您在故事板中所做的连接.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...