Sujay Khandekar
Sujay Khandekar

Reputation: 25

UILabels not appearing in custom UITableViewCell class

I have a bunch of UILabels that I'd like to add to my custom UITableViewCell class. I know how to do this with IBOutlets but I'd just like to know why doing it programmatically like this isn't working for me. Here's the code for my custom UITableViewCell class.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];
        [_nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:23.0]];
        [_nameLabel setTextColor:[UIColor blackColor]];
        [self addSubview:_nameLabel];

        _artistLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 91, 21)];
        [_artistLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:15.0]];
        [_artistLabel setTextColor:[UIColor blackColor]];
        _artistLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_artistLabel];    }
    return self;
}

Also I'll attach the code for the UITableView subclass that uses this cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SKItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    SKItemDoc *item = [self.items objectAtIndex:indexPath.row];

    cell.nameLabel.text = item.data.title;
    cell.artistLabel.text = item.data.artist;

    return cell;
}

Upvotes: 1

Views: 1166

Answers (3)

rdelmar
rdelmar

Reputation: 104092

If you made this cell in a storyboard or xib, then you need to override initWithCoder:, not initWithStyle:reuseIdentifier: (which won't be called).

Upvotes: 1

chancyWu
chancyWu

Reputation: 14423

in you custom cell init method. You need to add labels to self.contentView. change to

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];
        [_nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:23.0]];
        [_nameLabel setTextColor:[UIColor blackColor]];
        [self.contentView addSubview:_nameLabel];

        _artistLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 91, 21)];
        [_artistLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:15.0]];
        [_artistLabel setTextColor:[UIColor blackColor]];
        _artistLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_artistLabel];    }
    return self;
}

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89559

You need to create the cell if it didn't already exist, too.

E.G.:

SKItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(NULL == cell)
{
    cell = [[SKItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}

The style (UITableViewCellStyleDefault or whatever) doesn't matter as much as the fact you simply need to create some reusable cells.

Also, declare properties in your custom cell's .h file:

@property (strong) IBOutlet UILabel * nameLabel;
@property (strong) IBOutlet UILabel *artistLabel; 

and in the .m file:

@synthesize nameLabel = _nameLabel;
@synthesize artistLabel = _artistLabel;

Upvotes: 0

Related Questions