gpt4 book ai didi

ios - cellForRowAtIndexPath 和 heightForRowAtIndexPath 之间有什么关系

转载 作者:行者123 更新时间:2023-11-28 20:04:31 24 4
gpt4 key购买 nike

我在哪里使用在 heightForRowAtIndexPath 中计算的高度?

抱歉,如果文字不太清楚,这是希伯来语:)看,第二个单元格有 2 行?所以我希望牢房更高。所以,我根据文本计算了单元格的高度,然后去设置单元格。背景,例如,本应与单元格本身一样大,但它们都混在一起了……

enter image description here

代码如下:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
AnswerObject* answer = self.question.answers[indexPath.row];
CGFloat height = [AnswerTableViewCell answerCellHeight:answer];
return height;
}

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

AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AnswerTableViewCell alloc] init];
}

AnswerObject* answer = self.question.answers[indexPath.row];

[cell setupAnswerTableViewCell:self.question answer:answer row:indexPath.row];

return cell;
}

AnswerTableViewCell:

- (id)init
{
self = [super init];
if (self) {

// self.frame = CGRectMake(0, 0, 280, 77);

self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];

self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 20;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:16];
self.answerLabel.textAlignment = NSTextAlignmentRight;
self.answerLabel.lineBreakMode = NSLineBreakByWordWrapping;

self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];

self.backgroundImage = [[UIImageView alloc]init];
self.backgroundImage.frame = self.bounds;

[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage];
}
return self;
}


-(void)setupAnswerTableViewCell:(QuestionObject*)question
answer:(AnswerObject*)answer
row:(NSInteger)row{
self.question = question;
self.answer = answer;
self.row = row;

CGSize labelSize = [answer.answerText sizeWithFont:self.answerLabel.font constrainedToSize:CGSizeMake(self.answerLabel.frame.size.width, 100000) lineBreakMode:self.answerLabel.lineBreakMode];

// self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, labelSize.height + 20);

self.answerLabel.frame = CGRectMake(self.answerLabel.frame.origin.x, self.answerLabel.frame.origin.y, self.answerLabel.frame.size.width, labelSize.height);

self.answerLabel.text = answer.answerText;

[self.answerToggle addTarget:self
action:@selector(flip:)
forControlEvents:UIControlEventTouchDown];


self.answerToggle.tag = [answer.answerID intValue];

if (self.row == 0) {
self.backgroundImage.image = [UIImage imageNamed:@"List_Top_Item_Not_Selected_612x113px.png"];
}
else if (self.row == ([self.question.answers count] - 1)){
self.backgroundImage.image = [UIImage imageNamed:@"List_Bottom_Item_Not_Selected_612x113px.png"];
}
else{
self.backgroundImage.image = [UIImage imageNamed:@"List_Item_Not_Selected_612x113px.png"];
}

}

+(CGFloat)answerCellHeight:(AnswerObject*)answer{
CGSize labelSize = [answer.answerText sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:16] constrainedToSize:CGSizeMake(280, 100000) lineBreakMode:NSLineBreakByWordWrapping];

return labelSize.height + 20;
}

最佳答案

heightForRowAtIndexPath UITableView 向数据源询问它应该准备的单元格高度,然后才能填充其内容。

然后 tableView 在两个单元格之间分配那么大的大小/空间。通过那里的单元格仍然需要由数据源提供,这是在 cellForRowIndexPath 方法中完成的。

Edit

当您返回单元格的自定义高度时,您的框架重叠。但不在单元格中使用它们并将 subview 初始化为其他值。

您可以使用 layoutSubviews 获得适当的高度并正确布置您的 View 。或者在 setupAnswerCell 方法中使用额外的参数高度并传入自定义高度和在那里布置您的元素。

关于ios - cellForRowAtIndexPath 和 heightForRowAtIndexPath 之间有什么关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22538095/

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