Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell subclass doesn't let me set label text

I made a subclass of UITableViewCell to get a bigger TableViewCell with some more options.

But my problem is that I can't set the text(s) of the label(s):

BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]];

NSLog(@"%@", [bi title]);
[[cell mainLabel] setText:[bi title]];
NSLog(@"%@", [[cell mainLabel] text]);

The first log message returns the text I expected, but the second one always logs (null).

I really don't know what should be wrong. I've created the labels as usual:

@property (weak, nonatomic) IBOutlet UILabel *mainLabel;

Of course I connected the labels and synthesized them (checked it twice). I've also implemented the method

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

to get the appropriate height for each cell (which works fine).

BTW, checkmarks appear as expected. It's just about the labels.

like image 613
Jörg Kirchhof Avatar asked May 22 '26 02:05

Jörg Kirchhof


1 Answers

Make sure you are instantiating your custom UITableViewCell subclass in the cellForRowAtIndexPath method. Also make sure that those IBOutlets are declared in your UITableView subclass and NOT in the View Controller that houses the TableView. Also make sure that the parent class of your cell in interface builder is set to that same subclass.

Something like this (Custom UITableViewCell subclass interface file):

#import <UIKit/UIKit.h>

@interface MyCustomCell : UITableViewCell
    @property (nonatomic, weak) IBOutlet UILabel *mainLabel;
@end

Then @synthesize in the implementation file:

@synthesize mainLabel;

Then in cellForRowAtIndexPath, something like:

static NSString *CellIdentifier = @"MyCellIdentifier";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]];

// Configure the cell...
cell.mainLabel.text = [bi title];
// ... Other stuff
return cell;
like image 87
LJ Wilson Avatar answered May 23 '26 17:05

LJ Wilson