0

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

更新:修复如下

@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;
}

使固定

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

}
4

3 回答 3

1

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

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

于 2013-02-11T21:35:12.127 回答
0

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

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

于 2013-02-11T21:21:58.177 回答
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 中设置框架有什么不同,但它似乎对我有用。

于 2013-02-11T21:50:07.927 回答