• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:12:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我一直在阅读我在这里找到的其他人的解决方案,这些解决方案适用于具有动态高度和多行标签的单元格,但它们似乎都没有 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

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

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

谢谢。



Best Answer-推荐答案


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

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

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

更新!

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

关于ios - UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出错误的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32327680/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap