在哪里使用heightForRowAtIndexPath中计算的高度?
抱歉,如果文字不太清楚,则为希伯来语:)
看到,第二个单元格有2行?所以我希望单元格更高。
因此,我已经根据文本计算出单元格的高度,然后去设置单元格。例如,背景本来应该是单元格本身的大小,并且混合在一起了……
这是代码:
-(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
方法完成的。
编辑
当您返回单元格的自定义高度时,框架重叠。但不要在单元格中使用它们并将子视图初始化为其他值。
您可以使用layoutSubviews
来获得适当的高度,并正确地放置视图。.或在setupAnswerCell
方法中使用额外的参数高度,然后传入自定义高度并在其中布局项目。
关于ios - cellForRowAtIndexPath和heightForRowAtIndexPath之间是什么关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22538095/