UITableView 奇怪的布局行为在滚动时发生变化 [英] UITableView strange layout behavior changes on scroll

查看:31
本文介绍了UITableView 奇怪的布局行为在滚动时发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 UITableViewCellUITableView 设计为具有自动高度.

I have a UITableView with custom UITableViewCell designed to have automatic height.

行为是在我的 UIViewControllerUITableView 的第一次加载中显示带有部分文本的标签.然后当我滚动到底部然后滚动到顶部时,我得到了所需的高度和文本外观.

The behavior is that in the first load of my UIViewController with UITableView displays my labels with partial texts. Then when I scroll to bottom then scroll to top, I get the desired look for height and for texts.

截图如下:

初始查找视图控制器:

这是滚动底部然后顶部后的外观(这是我的实现的正确外观,我删除了一些 Lorem 文本):

Here is the look after scrolling bottom then top (This is the correct look for my implementation some Lorem text is removed by me):

编辑 1:

代码:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 101 // much heigher than most cells. But no use.

在 UI Builder 中,UITableViewCell 的行高"设置为默认"(未选中自定义)

In the UI Builder, the UITableViewCell's "Row Height" set to "Default" (Custom not checked)

在 UI Builder 中,UITableView 的行高"设置为101"(也尝试将其设置为0"但结果相同.)

In the UI Builder, the UITableView's "Row Height" set to "101" (also tried to set it to "0" but same result.)

推荐答案

让自动布局在 UITableViewCell 上工作是为了确保你有约束来固定每个子视图的所有边——也就是说,每个子视图应该有前导、顶部、尾随和底部约束.

Getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.

此外,您需要一条清晰的约束线,从 contentView 的顶部到底部.这确保自动布局根据其子视图正确确定 contentView 的高度.

Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.

棘手的部分是,如果您缺少其中一些约束,Interface Builder 通常不会警告您;当您运行项目时,自动布局根本不会返回正确的高度.例如,它可能会为单元格高度返回 0,这表明您的约束需要更多工作.

The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.

如果您在处理自己的项目时遇到问题,请尝试调整您的约束,直到满足上述条件.

If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.

查看链接了解详情:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

这篇关于UITableView 奇怪的布局行为在滚动时发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆