gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:08:26 25 4
gpt4 key购买 nike

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

更新:修复如下

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

}

最佳答案

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

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

关于iphone - 自定义 UITableViewCell 为空,尽管 heightForRowAtIndexPath 中的单元格是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14820868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com