【发布时间】: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:)
标签: ios uitableview uilabel