我从远程网址获取 UILabel 文本,有时文本太短/太长,我需要调整框架大小以适应文本高度。
我已经使用 xib
实现了服装单元。
这是我到目前为止所尝试的,它应该有效,但由于我无法理解的原因而无效:
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/