gpt4 book ai didi

ios - UITableViewCell 仅在滚动时正确布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:35 27 4
gpt4 key购买 nike

我有一个带有自定义 UITableViewCellsUITableView。根据我从服务器收到的文本,单元格需要具有不同的高度。我相应地改变高度如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return [self getHeightOfQuestion];
else if (indexPath.row == 1)
return 64.0f;
else // ****** this is the cell in question!
return [self getHeightOfAnswerAtIndexPath:indexPath];
return 300.0f;
}

]- (CGFloat) getHeightOfAnswerAtIndexPath:(NSIndexPath*) indexPath
{
CGRect answerFrame = CGRectZero;
if ([QXTUtility currentOsVersion] >= 7.0f)
{
answerFrame = [[[[self.answerDetails objectForKey:kAnswers] objectAtIndex:indexPath.row-2] objectForKey:kAnswerText] boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-Light" size:14.0f]} context:nil];
}
else
{
CGSize answerSize = [[[[self.answerDetails objectForKey:kAnswers] objectAtIndex:indexPath.row-2] objectForKey:kAnswerText] sizeWithFont:[UIFont fontWithName:@"Roboto-Light" size:14.0f] constrainedToSize:CGSizeMake(242.0f, 500.0f) lineBreakMode:NSLineBreakByTruncatingTail];
answerFrame.size = answerSize;
}
NSLog(@"Frame %@", NSStringFromCGRect(answerFrame));

return answerFrame.size.height + 50;
}

它在 iOS6 上运行良好。但在 iOS7 中,我得到以下输出:

enter image description here

但是,如果我再次滚动 UITableView,单元格的布局就像这样:

enter image description here

这是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *answerCellIdentifier = @"QXTAnswerCell";

QXTAnswerCell *answerCell = (QXTAnswerCell *)[tableView dequeueReusableCellWithIdentifier:answerCellIdentifier];
if (answerCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QXTAnswerCell" owner:self options:nil];
answerCell = [nib objectAtIndex:0];
}
[self setDataToAnswerCell:answerCell AtIndexPath:indexPath];
[self layoutAnswerCell:answerCell AtIndexPath: indexPath];
[answerCell setNeedsLayout];
return answerCell;

- (void) layoutAnswerCell:(QXTAnswerCell*) answerCell
AtIndexPath:(NSIndexPath*) indexPath
{
CGRect answerCellFrame = answerCell.answer.frame;
answerCellFrame.origin.y = answerCell.name.frame.origin.y + answerCell.name.frame.size.height + 6;
if ([QXTUtility currentOsVersion] >= 7.0f)
{
answerCellFrame.size.height += 30.0f;
answerCell.answer.lineBreakMode = NSLineBreakByWordWrapping;
}
answerCell.answer.frame = answerCellFrame;
dispatch_async(dispatch_get_main_queue(), ^{
[answerCell setNeedsLayout];
});
}

请帮帮我。填充答案的数据源后,我将调用重新加载数据。谢谢。

更新

刚发现第一个 reloadData 在从服务器收到答案后不会被调用。

// Delegate from Network Class
- (void) answersReceivedWithDetails:(id)answerDetails
{
NSLog(@"Answer Details %@", answerDetails);
self.answerDetails = [[NSMutableDictionary alloc] initWithDictionary:answerDetails];
[self.tableView reloadData];
[self.tableView setNeedsDisplay];
}

这是单元格的约束:

enter image description here

最佳答案

以下是我为解决问题所做的工作。使用 IBOutlets 创建的 boundingRectWithSizeUILabels 存在一些问题。因此,我执行了上述操作,然后将其分配给使用代码创建的 UILabel 并将其添加到单元格中。一切都运行良好!

- (void) layoutAnswerCell:(QXTAnswerCell*) answerCell
AtIndexPath:(NSIndexPath*) indexPath
{
if ([QXTUtility currentOsVersion] >= 7.0f)
{
[answerCell.answer removeFromSuperview];
NSString *answerString = <your string>
CGRect answerFrame = [answerString boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-Light" size:14.0f]} context:nil];
answerFrame.origin.x = 20.0f;

answerFrame.origin.y = answerCell.name.frame.origin.y + answerCell.name.frame.size.height + 6;
answerFrame.size.height = ceilf(answerFrame.size.height);
answerFrame.size.height += 30.0f;
answerCell.answer.lineBreakMode = NSLineBreakByTruncatingTail;
answerCell.answer.frame = answerFrame;

UILabel *newLabel = [[UILabel alloc] initWithFrame:answerFrame];
[newLabel setFont:[UIFont fontWithName:@"Roboto-Light" size:14.0f]];
newLabel.text = answerString;
newLabel.numberOfLines = 0;
[answerCell.contentView addSubview:newLabel];
[answerCell setNeedsDisplay];
}
}

看看这对其他人是否有效。如果是这样,我会接受这个答案。但它确实帮我完成了工作。

关于ios - UITableViewCell 仅在滚动时正确布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20797179/

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