【发布时间】:2013-11-29 10:55:08
【问题描述】:
我创建了一个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 似乎一切正常。为什么?
【问题讨论】:
标签: ios uitableview storyboard subclass