ToddB
ToddB

Reputation: 2520

Custom UITableViewCell is empty, although cell from heightForRowAtIndexPath is correct

Sorry, I am missing it. I have a beautiful table with correct (variable) row heights. But all the cells are blank. Three labels should be populated in each cell.

UPDATE: FIX IS BELOW

@implementation MasterTableCell 

@synthesize labelDesc;
@synthesize labelDuration;
@synthesize labelName;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {


    // name
    CGRect frameTextLabel = CGRectMake(8, 2, 244, 25);
    labelName = [[UILabel alloc] initWithFrame:frameTextLabel];
    labelName.lineBreakMode = UILineBreakModeTailTruncation;
    labelName.font = [UIFont boldSystemFontOfSize:17.0];
    labelName.textColor = [UIColor blackColor];

    // description
    labelDesc = [[UILabel alloc] init];
    labelDesc.lineBreakMode = UILineBreakModeWordWrap;
    labelDesc.numberOfLines = 0;
    labelDesc.textColor = [UIColor grayColor];
    labelDesc.font = [UIFont systemFontOfSize:14.0f];
    labelDesc.backgroundColor = [UIColor blueColor];

    // duration
    CGRect frame = CGRectMake(252, 5, 40, 20);
    labelDuration = [[UILabel alloc] initWithFrame:frame];
    labelDuration.font = [UIFont boldSystemFontOfSize:14.f ];
    labelDuration.textAlignment = UITextAlignmentRight;
    labelDuration.backgroundColor = [UIColor redColor]; // to see it

    [self.contentView addSubview:labelName];
    [self.contentView addSubview:labelDesc];
    [self.contentView addSubview:labelDuration];

  }
  return self;
}

@end

And

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"RecipeCell";

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

  Recipe *recipeAtIndex = [sortedArray objectAtIndex:indexPath.row];

  cell.labelName.text = @"Test1";
  cell.labelDesc.text = @"Test2";
  cell.labelDuration.text = [TBCommon formattedStringforDuration:recipeAtIndex.duration withDelimeter:@":"];

  CGRect frameDescLabel = CGRectMake(8, 25, 284, [self heightForDescriptionFrame:recipeAtIndex.description]);
  cell.labelDesc.frame = frameDescLabel;

  return cell;
}

FIX

#import "MasterTableCell.h"

@implementation MasterTableCell : UITableViewCell

@synthesize labelDesc;
@synthesize labelDuration;
@synthesize labelName;

- (void)awakeFromNib
{
    [super awakeFromNib];

// name
CGRect frameTextLabel = CGRectMake(8, 2, 244, 25);
labelName = [[UILabel alloc] initWithFrame:frameTextLabel];
labelName.lineBreakMode = UILineBreakModeTailTruncation;
labelName.font = [UIFont boldSystemFontOfSize:17.0];
labelName.textColor = [UIColor blackColor];

// description
labelDesc = [[UILabel alloc] init];
labelDesc.lineBreakMode = UILineBreakModeWordWrap;
labelDesc.numberOfLines = 0;
labelDesc.textColor = [UIColor grayColor];
labelDesc.font = [UIFont systemFontOfSize:14.0f];

// duration
CGRect frame = CGRectMake(252, 5, 40, 20);
labelDuration = [[UILabel alloc] initWithFrame:frame];
labelDuration.textColor = [UIColor colorWithRed:38.0/255.0 green:111.0/255.0 blue:208.0/255.0 alpha:1];
labelDuration.font = [UIFont boldSystemFontOfSize:14.f ];
labelDuration.textAlignment = UITextAlignmentRight;

[self.contentView addSubview:labelName];
[self.contentView addSubview:labelDesc];
[self.contentView addSubview:labelDuration];
}

@end

And

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

static NSString *CellIdentifier = @"Cell";

MasterTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Recipe *recipeAtIndex = [sortedArray objectAtIndex:indexPath.row];

cell.labelName.text = recipeAtIndex.name;
cell.labelDesc.text = recipeAtIndex.description;
cell.labelDuration.text = [TBCommon formattedStringforDuration:recipeAtIndex.duration withDelimeter:@":"];

CGRect frameDescLabel = CGRectMake(8, 25, 284, [self heightForDescriptionFrame:recipeAtIndex.description]);
cell.labelDesc.frame = frameDescLabel;

return cell;

}

Upvotes: 0

Views: 2508

Answers (3)

GeneralMike
GeneralMike

Reputation: 3001

When I was setting up my custom cell subclasses, the tutorial I was following had me set the frames in a separate method called layoutSubviews. For you, it would look like

- (void) layoutSubviews
{
    [super layoutSubviews];

    labelName.frame = CGRectMake(8, 2, 244, 25);
    labelDuration.frame = CGRectMake(252, 5, 40, 20);
}

When I had something to add to a cell that is dynamically located like labelDesc is, I had to totally pull it out of my custom cells class and put it all in cellForRowAtIndexPath (so I would have something like labelDesc = [[UILabel alloc] initWithFrame... and so on all in cellForRowAtIndexPath, and nothing for it in the initWithStyle).

I don't know why doing it this way is any different than setting the frames in initWithStyle, but it seems to be working for me.

Upvotes: 0

jsd
jsd

Reputation: 7693

If you're using Storyboards, initWithStyle will never be called. Move the label creation code into awakeFromNib.

You can get rid of the whole if (cell == nil) part too because dequeueReusableCell will ALWAYS return a cell.

Upvotes: 1

puru020
puru020

Reputation: 818

Do you think that your table view delegate/datasource method are actually being called. Trying putting a break point in cellForRowAtIndexPath method to verify that. If breakpoint's not hit it means your delegate and datasource are not set. Set those.

Another thing that I noticed is that you have not set the frame for your labelDesc instance variable. That's worth looking at.

Upvotes: 0

Related Questions