我有一个方案,其中一个UITableViewCell
有2个UILable
。我可以从服务器中获取长文本,这会使两个标签同时成为多行。
我已经实现了autolayout和
label.numberofline = 0;
tableVieww.estimatedRowHeight = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;
但它只会导致一个标签扩展而不是两者。请帮忙。
提前致谢
答案 0 :(得分:3)
试试这个:
使用此代码计算行高
注意:ExtraHeight“H”
假设您的单元格高度 100 ,并且在该单元格中标签的高度 20 ,而额外的高度将 80 ,因为此代码正在根据文本大小计算标签的高度。
你要把这段代码放在 heightforRowatindexpath 中,所以 cell.label 无法到达那里,所以只需传递文字并使用 Method-2 强>
方法 - 1:
-(CGFloat)setRowHeightWithLabel:(UILabel *)lbl withLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
{
CGFloat totalHeight;
CGFloat lblWidth = lbl.frame.size.width;
CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
if (lblTextSize.height > lbl.frame.size.height) {
totalHeight = (lblTextSize.height+H);
}
else
{
totalHeight = lbl.frame.size.height+H;
}
NSLog(@"+++++++++++++Height - %.2f",totalHeight);
return totalHeight;
}
将此用于 heightforRow 方法
方法 - 2:
-(CGFloat)setRowHeightWithLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
{
CGFloat totalHeight;
CGFloat lblWidth = 300;
CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
if (lblTextSize.height > 20) {
totalHeight = (lblTextSize.height+H);
}
else
{
totalHeight = 20+H;
}
NSLog(@"+++++++++++++Height - %.2f",totalHeight);
return totalHeight;
}
答案 1 :(得分:1)
答案 2 :(得分:0)
到目前为止你看起来很好。这应该可以在不需要像某些人建议的那样计算标签高度的情况下工作。
确保您的标签上有适当的约束。
我测试了它。它正在工作!
- (void)viewDidLoad {
[super viewDidLoad];
dynamicTableVuew.rowHeight = UITableViewAutomaticDimension;
dynamicTableVuew.estimatedRowHeight = 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
UILabel *label1 = (UILabel *)[cell viewWithTag:1];
UILabel *label2 = (UILabel *)[cell viewWithTag:2];
label1.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";
label2.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";
return cell;
}
这就是我在视图控制器中所拥有的一切。故事板中没什么特别的。
要检查的最后一件事。确保您的tableview通过插座连接到视图控制器。您可能会看到那里的插座代码,但并不意味着它已连接。也许它被误删了。以下几行不会生效:
tableVieww.estimatedRowHeight = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;