我创建了一个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
开始一切都正常。为什么?