gpt4 book ai didi

iphone - sizeWithFont 方法的神秘问题

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

伙计们。我有带有不同单元格的 UITableView,我有计算高度的代码。在一个项目中它运行完美,但在第二个项目中它返回高度等于 0。是什么原因造成的?我的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat cellWidth = 320.0f;
CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cellWidth, CGFLOAT_MAX) lineBreakMode:cell.textLabel.lineBreakMode];
CGFloat height = size.height;
NSLog(@"Height: %f", height);
return height;
}

最佳答案

heightForRowAtIndexPath: 委托(delegate)方法在 cellForRowAtIndexPath: 委托(delegate)方法之前被调用。在您的代码中,您正在根据 cell.textLabel.text 计算单元格高度,但尚未设置单元格中的文本。

您需要从表格单元格以外的其他地方获取要在此方法中使用的文本(当您在 cellForRowAtIndexPath 中设置 textlabel.text 值时,大概可以从任何地方获取它)。

关于iphone - sizeWithFont 方法的神秘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8865366/

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