自定义UITableViewCell为空,尽管来自heightForRowAtIndexPath的单元格是正确的

时间:2013-02-11 21:07:31

标签: iphone ios uitableview

抱歉,我错过了。我有一张漂亮的桌子,有正确的(可变的)行高。但是所有细胞都是空白的。每个单元格中应填充三个标签。

更新:修正如下

@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

-(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

-(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;

}

3 个答案:

答案 0 :(得分:1)

如果您正在使用Storyboard,则永远不会调用initWithStyle。将标签创建代码移动到awakeFromNib。

你也可以摆脱整个if(cell == nil)部分,因为dequeueReusableCell总是会返回一个单元格。

答案 1 :(得分:0)

您是否认为实际调用了表视图委托/数据源方法?尝试在cellForRowAtIndexPath方法中设置一个断点来验证。如果断点未命中,则表示您的委托和数据源未设置。设置那些。

我注意到的另一件事是你没有为labelDesc实例变量设置框架。值得一看。

答案 2 :(得分:0)

当我设置自定义单元格子类时,我所遵循的教程让我在一个名为layoutSubviews的单独方法中设置框架。对你而言,它看起来像

- (void) layoutSubviews
{
    [super layoutSubviews];

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

当我有一些东西需要添加到动态定位的单元格labelDesc时,我必须完全将它从我的自定义单元格类中拉出来并将它全部放在cellForRowAtIndexPath中(所以我会在labelDesc = [[UILabel alloc] initWithFrame...中有cellForRowAtIndexPath等所有内容,initWithStyle中没有任何内容。

我不知道为什么这样做与在initWithStyle中设置帧有什么不同,但它似乎对我有效。