Clay
Clay

Reputation: 43

Label won't display text after subclassing UITableViewCell

So I know that is was asked a million times. Maybe Im too tired, but I cant figure out why this label text wont update. Heres My Code

CustomCell.h

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell {
    UILabel *mainLabel; 
    UILabel *leftBottomLabel;
    UILabel *rightBottomLabel;

}


@property (nonatomic, retain) UILabel *mainLabel;
@property (nonatomic, retain) UILabel *leftBottomLabel;
@property (nonatomic, retain) UILabel *rightBottomLabel;


@end

CustomCell.m

#import "CustomCell.h"


@implementation CustomCell

@synthesize mainLabel;
@synthesize leftBottomLabel; 
@synthesize rightBottomLabel;



-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // Initialization code


        UIView *myContentView = self.contentView;



        self.mainLabel.textAlignment = UITextAlignmentLeft; 
        [myContentView addSubview:self.mainLabel];
        [self.mainLabel release];



        self.leftBottomLabel.textAlignment = UITextAlignmentLeft; 
        [myContentView addSubview:self.leftBottomLabel];
        [self.leftBottomLabel release];


        self.rightBottomLabel.textAlignment = UITextAlignmentLeft; // default
        [myContentView addSubview:self.rightBottomLabel];
        [self.rightBottomLabel release];
    }


    return self;
}


- (void)layoutSubviews {

    [super layoutSubviews];


    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;


        frame = CGRectMake(boundsX + 10, 4, 200, 20);
        self.mainLabel.frame = frame;
        self.mainLabel.backgroundColor = [UIColor redColor];

        frame = CGRectMake(boundsX + 10, 28, 200, 20);
        self.leftBottomLabel.frame = frame;
        self.leftBottomLabel.backgroundColor = [UIColor greenColor];


        frame = CGRectMake(boundsX + 100, 28, 200, 14);
        self.rightBottomLabel.frame = frame;
        self.rightBottomLabel.backgroundColor = [UIColor blueColor];

}



- (void)dealloc {

    [mainLabel dealloc];
    [leftBottomLabel dealloc];
    [rightBottomLabel dealloc];
    [super dealloc];
}

@end

MyView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

    cell.mainLabel.text = @"Hello";
return cell;
}

Any thoughts? I get nothing in this label, don't know why.

Upvotes: 2

Views: 1021

Answers (2)

Benjamin Mayo
Benjamin Mayo

Reputation: 6679

You need to initially create each three labels, before you can change their properties or get any screen drawing to occur. At the moment, these three labels have only been declared. To fix, in your initWithStyle:reuseIdentifier:, add the following at the top:

self.mainLabel = [[UILabel alloc] init];
self.leftBottomLabel = [[UILabel alloc] init];
self.rightBottomLabel = [[UILabel alloc] init];

Your layoutSubviews code will handle the frame changes (which is why you dont need [[UILabel alloc] initWithFrame..] to initialise, as you set the frame yourself later) ' at the appropriate time, and your labels should now display correctly.

Upvotes: 3

Nils Munch
Nils Munch

Reputation: 8845

You never seem to actually create the label.

You need a

mailLabel = [[UILabel alloc] initwithframe...]

in your init code.

Upvotes: 1

Related Questions