我需要创建一个显示四个UITableViewCell
个实例的自定义UILabel
。
在这个自定义单元格的实现中,我有以下初始化代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// configure labels
self.positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 300, 30)];
self.positionLabel.textColor = [UIColor whiteColor];
self.positionLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0f];
[self addSubview:self.positionLabel];
}
return self;
}
在我的cellForRowAtIndexPath
方法中,我有以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomUICell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if (cell == nil) {
cell = [[CustomUICell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
cell.positionLabel.text = @"Test";
}
[cell setBackgroundColor:[UIColor clearColor]];
return cell;
}
但是,我不想使用initWithFrame:CGRectMake(5, 10, 300, 30)];
创建单元格,而是希望在我的故事板中执行此操作。如果我将标签拖到故事板中的单元格上,并将其连接到相应的属性,如下所示:
@property (nonatomic) IBOutlet UILabel *positionLabel;
并通过写:
创建我的标签self.positionLabel = [[UILabel alloc] init];
当我运行我的应用程序时,我希望在我的单元格中看到一个标签,但这并不会发生。有没有人对我错过/不理解的内容有任何见解?谢谢!
答案 0 :(得分:2)
首先,使用dequeueReusableCellWithIdentifier:forIndexPath:
代替dequeueReusableCellWithIdentifier:
。在这种情况下,您不需要if
子句。
关于您的问题:在故事板中添加单元格时,您无需实施initWithStyle:reuseIdentifier:
。实际上,如果这样做,代码路径就会被执行并覆盖你在故事板中所做的一切。