UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出错误的高度

Posted

技术标签:

【中文标题】UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出错误的高度【英文标题】:Multiline UILabel in a UITableViewCell giving the wrong height with autoLayout in iOS8 【发布时间】:2015-09-01 09:10:01 【问题描述】:

我一直在阅读我在这里找到的其他人的解决方案,这些解决方案适用于具有动态高度和多行标签的单元格,但它们似乎都没有 100% 工作。

Cell 具有以下垂直约束:Vertical cell constraints

tableView配置为:

self.tableView.estimatedRowHeight = 100;
self.tableView.rowHeight = UITableViewAutomaticDimension;

在方法tableview:cellForRowAtIndexPath:中我设置了多行标签的preferredMaxLayoutWidth:

JBLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JBLabelTableViewCell" forIndexPath:indexPath];
NSString *labelText = [self.items objectAtIndex:indexPath.item];
[cell.bodyLabel setText:labelText];
CGRect cellFrame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 99999);
[cell setBounds:cellFrame];
cell.contentView.bounds = cell.bounds;
[cell layoutIfNeeded];
[cell.bodyLabel setPreferredMaxLayoutWidth:CGRectGetWidth(cell.bodyLabel.frame)];
return cell;

数据源是用这两个字符串设置的:

_items = @[@"On this screen you can see the groups of people that you have set yourself up to chat with.\n\nCommunities are groups of people that are created for you on the basis of your interests, and how you feel about your condition, or the conditions of those you care about.\n\nThey are not only there to provide you with help or someone to talk to but can also benefit from the help and support that you can give.",
               @"On this screen you can see a list of the conversations that you are involved in.\n\nWithin conversations you can ask questions to like-minded people or just have someone to chat to. These are places where you can help someone or get someone to help you."];

在 iPhone 6 模拟器中运行项目,第一个字符串的最后一行被截断:iPhone screenshot

所有对象之间的约束看起来都正确,但多行标签的计算高度不适用于第一个字符串。

有没有人遇到过这个问题并有解决方案?

谢谢。

【问题讨论】:

我也尝试过用 ios6 和 iOS7 的方式返回 tableView 中的单元格高度:heightForRowAtIndexPath:[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] 您可以保留并更新对 bodyLabel height 的约束,该约束在文本更改时设置为 sizeThatFits: 谢谢@kdogisthebest,我刚试过你的建议,结果是一样的。在我看来,计算标签高度的框架的任何函数在某些情况下都会为多行标签提供更短的高度(我也尝试过 NSString 方法:boundingRectWithSize:options:attributes:context: 【参考方案1】:

您的代码可能是正确的。我只是添加了这两行(不必说。从您已经添加的屏幕截图的外观来看!)

cell. bodyLabel.numberOfLines = 0;
cell. bodyLabel.lineBreakMode = NSLineBreakByWordWrapping;

看来您的问题在于您的用户界面。这是我用于 UITableViewCell 的用户界面,它工作得非常好

更新!

只需将其添加到您的 viewDidLoad [self.view layoutIfNeeded]; [self.view setNeedsLayout]; 在 viewDidLoad 中,用户界面的框架正在被使用,您需要更新约束和视图以使用设备屏幕框架 layoutIfNeeded 和 setNeedsLayout 的顺序很重要。你需要先调用 layoutIfNeeded。

【讨论】:

这个问题并不总是发生,它取决于屏幕大小和字符串大小。此外,如果字体类型/大小和约束与我的不同,那么它可能不会重现。我的项目中有类似的限制。我在这里分享一个指向我使用的示例项目的链接,以防有人想看看:dropbox.com/s/gpjcdny50m37on3/label.zip?dl=0 只需将其添加到您的 viewDidLoad [self.view layoutIfNeeded]; [self.view setNeedsLayout];在 viewDidLoad 中,用户界面的框架正在被使用,您需要更新约束和视图以使用设备屏幕框架 layoutIfNeeded 和 setNeedsLayout 的顺序很重要。你需要先调用 layoutIfNeeded 感谢@user962913 将这两行添加到 viewDidLoad 确实可以解决问题。您可以使用修复更新您的答案,以便其他人可以更快地找到解决方案。

以上是关于UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出错误的高度的主要内容,如果未能解决你的问题,请参考以下文章

自定义 UITableviewCell 中的多行标签

UItableviewcell中的多行UIlabel使用Autolayout

在 UITableViewCell 中的 detailTextLabel 中添加多行

iOS 9 上的静态 UITableViewCell 中的多行 UILabel

UITableViewCell 中的几个多行标签,情节提要 xcode 6.3.2 中的自动布局

UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出错误的高度