UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出了错误的高度
Multiline UILabel in a UITableViewCell giving the wrong height with autoLayout in iOS8
我一直在阅读其他人的解决方案,我在这里找到了具有动态高度和多行标签的单元格,但 none 似乎 100% 有效。
单元格具有以下垂直约束: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
所有对象之间的约束看起来都正确,但多行标签的计算高度对于第一个字符串不正确。
有没有人运行解决这个问题并有解决方案?
谢谢。
你的代码大概是correct.i刚加了这两行(不用说了,从你的截图看你已经加了!)
cell. bodyLabel.numberOfLines = 0;
cell. bodyLabel.lineBreakMode = NSLineBreakByWordWrapping;
看来您的问题出在您的用户界面上。这是我用于 UITableViewCell 的用户界面,它工作得很好
更新!
只需将其添加到您的 viewDidLoad [self.view layoutIfNeeded]; [self.view setNeedsLayout];
在 viewDidLoad 中,正在使用用户界面的框架,您需要更新约束和视图以使用设备屏幕框架。layoutIfNeeded 和 setNeedsLayout 的顺序很重要。你需要先调用 layoutIfNeeded.
我一直在阅读其他人的解决方案,我在这里找到了具有动态高度和多行标签的单元格,但 none 似乎 100% 有效。
单元格具有以下垂直约束: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
所有对象之间的约束看起来都正确,但多行标签的计算高度对于第一个字符串不正确。
有没有人运行解决这个问题并有解决方案?
谢谢。
你的代码大概是correct.i刚加了这两行(不用说了,从你的截图看你已经加了!)
cell. bodyLabel.numberOfLines = 0;
cell. bodyLabel.lineBreakMode = NSLineBreakByWordWrapping;
看来您的问题出在您的用户界面上。这是我用于 UITableViewCell 的用户界面,它工作得很好
更新!
只需将其添加到您的 viewDidLoad [self.view layoutIfNeeded]; [self.view setNeedsLayout]; 在 viewDidLoad 中,正在使用用户界面的框架,您需要更新约束和视图以使用设备屏幕框架。layoutIfNeeded 和 setNeedsLayout 的顺序很重要。你需要先调用 layoutIfNeeded.