gpt4 book ai didi

ios - 更改 tableView 单元格内的 UILabel 框架高度

转载 作者:行者123 更新时间:2023-11-29 01:08:41 25 4
gpt4 key购买 nike

我从远程网址获取 UILabel 文本,有时文本太短/太长,我需要调整框架大小以适应文本高度。我已经使用 xib 实现了服装单元。

screenshot

这是我到目前为止所尝试的,它应该有效,但由于我无法理解的原因而无效:

 cell.fbLastObject.text = [NSString stringWithFormat:@"%@",update.fbLastObject];
[cell.fbLastObject sizeToFit];

//Cell Size
CGSize labelSize = [cell.fbLastObject.text sizeWithFont:cell.fbLastObject.font
constrainedToSize:cell.fbLastObject.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
labelHeight = labelSize.height;
cell.fbLastObject.frame = CGRectMake(0,0,300,labelHeight);

有人知道吗?

最佳答案

对于 iOS 8 及更高版本,您可以使用 UITableViewAutomaticDimension 将以下代码放在 viewDidLoad 中:

  tableView.rowHeight = UITableViewAutomaticDimension;
/* any estimated height but must be more than 2 */
tableView.estimatedRowHeight = 160.0;
tableview.delegate = self;
tableview.dataSource = self;
// you have created the SimpleTableViewCell.xib, So you need to register that cell with table.
UINib *nib = [UINib nibWithNibName:@"SimpleTableViewCell" bundle:nil];
[tableview registerNib:nib forCellReuseIdentifier:@"SimpleTableViewCell"];

现在将适当的 cell fbLastObject 约束(顶部、底部、前导和尾随)引脚添加到 super View 并将行数设置为 0

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

SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// here set text for your cell label
cell.fbLastObject.text = @"My Text";

[cell setNeedsLayout];
[cell layoutIfNeeded];

return cell;
}

关于ios - 更改 tableView 单元格内的 UILabel 框架高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36012760/

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