Reputation: 3221
I need to create a custom UITableViewCell
that displays four UILabel
instances.
In the implementation of this custom cell, I have the following initialization code:
- (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;
}
In my cellForRowAtIndexPath
method, I have the following code to
- (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;
}
However, instead of creating the cells with initWithFrame:CGRectMake(5, 10, 300, 30)];
, I would prefer to do this in my storyboard. If I drag a label onto my cell in the storyboard, and connect it to the appropriate property as below:
@property (nonatomic) IBOutlet UILabel *positionLabel;
and create my label by writing:
self.positionLabel = [[UILabel alloc] init];
I would expect to see a label in my cell when I run my app, but that doesn't happen. Does anyone have any insight into what I'm missing / not understanding? Thank you!
Upvotes: 0
Views: 33
Reputation: 14063
First, use dequeueReusableCellWithIdentifier:forIndexPath:
instead of dequeueReusableCellWithIdentifier:
. In this case you don't need the if
clause.
About your question: When you add the cell in a storyboard, you don't need to implement initWithStyle:reuseIdentifier:
. In fact, if you do, the code path is executed and overrides everything you do in the storyboard.
Upvotes: 2