gpt4 book ai didi

iphone - 何时捕获 UILabel 的高度?

转载 作者:行者123 更新时间:2023-11-29 05:05:10 24 4
gpt4 key购买 nike

我正在尝试在实例变量中捕获 UILabel 的高度。我使用的是默认样式 UITableViewCellStyleValue2

到目前为止,我 try catch 创建单元格时的高度

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

代码(为简洁起见,跳过了双端队列的内容):

UITableViewCell *testCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"myCell"] autorelease];

testCell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
testCell.detailTextLabel.numberOfLines = 10;

testCell.selectionStyle = UITableViewCellSelectionStyleNone;

testCell.textLabel.text = @"LabelName";
testCell.detailTextLabel.text = [object valueForKey:@"key"];
[testCell.detailTextLabel sizeToFit]; //still says 0.00 :-(

labelHeight = testCell.detailTextLabel.bounds.size.height;
NSLog(@"testCell height is %f", testCell.detailTextLabel.bounds.size.height); // equals 0.00
NSLog(@"cellFrame is %f", testCell.frame); //also says 0.00
return cell;

NSLog 指出高度为 0.00。

那么,哪里是捕获标签高度的好地方?或者说,我错过了什么?我认为当我分配 [someObject valueForKey:theKey] 时,会设置 detailTextLabel 边界。谢谢!

编辑:完整代码。

最佳答案

请将所有代码发布到 cellForRowAtIndexPath 中。我怀疑你没有给detailTextLabel一个非零的框架(或边界)。您还可以 NSLog the cell.frame 以查看它的宽度和高度。

关于iphone - 何时捕获 UILabel 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5680862/

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