0

申し訳ありませんが、私はそれを逃しています。正しい(可変の)行の高さを持つ美しいテーブルがあります。しかし、すべてのセルは空白です。各セルに3つのラベルを入力する必要があります。

更新:修正は以下にあります

@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

ストーリーボードを使用している場合、initWithStyleが呼び出されることはありません。ラベル作成コードをawakeFromNibに移動します。

dequeueReusableCellは常にセルを返すため、if(cell == nil)部分も全体を取り除くことができます。

于 2013-02-11T21:35:12.127 に答える
0

テーブルビューデリゲート/データソースメソッドが実際に呼び出されていると思いますか。それを確認するために、cellForRowAtIndexPathメソッドにブレークポイントを設定しようとしています。ブレークポイントがヒットしない場合は、デリゲートとデータソースが設定されていないことを意味します。それらを設定します。

私が気付いたもう1つのことは、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 に答える