UITableView奇怪的布局行为在滚动时发生变化
我有一个 UITableView
,其自定义 UITableViewCell
设计为具有自动高度。
I have a UITableView
with custom UITableViewCell
designed to have automatic height.
行为是在我的 UIViewController
的第一次加载中,带有 UITableView
显示我的标签部分文本。然后当我滚动到底部然后滚动到顶部,我得到所需的高度和文本外观。
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.
以下是截图:
视图控制器的初始查看:
Initial look for the view controller:
这是滚动底部然后顶部的外观(这是我的实现的正确外观,我删除了一些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
check the link for details:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift