[英]How to use dynamic height of UITableViewCell in auto-layout and move other views to up when bottom view is hidden?
I have a UITableViewCell in xib and its outlets in corresponding UITableViewCell subclass.我在 xib 中有一个 UITableViewCell,在相应的 UITableViewCell 子类中有它的插座。 I am returning height of cell from我正在返回单元格的高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {
return 400
}
I need to hide some views based on the data available in each row of the table and bottom view should shifted to top of the cell.我需要根据表格每一行中的可用数据隐藏一些视图,底部视图应该移到单元格的顶部。 When I am hiding view from cell then there is empty space left in place of hidden view & bottom views are not shifting to top part of the cell.当我从单元格隐藏视图时,会留下空白空间代替隐藏视图,底部视图不会转移到单元格的顶部。
Here is How I am hiding cell view.这是我如何隐藏单元格视图。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
.....
cell.opetion4.isHidden = true
cell.opetion3.isHidden = true
}
This is my cell.这是我的手机。
After hide 2 middle labels it is looking as follows.隐藏 2 个中间标签后,它看起来如下。
But I want to remove this empty space and want to shift bottom label to top as follows.但我想删除这个空白区域并希望将底部标签移到顶部,如下所示。
At first, make the height of UITableViewCell to UITableView.automaticDimension
首先,将UITableViewCell的高度设为UITableView.automaticDimension
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Embed all of your questionLabels in a UIStackView
(vertical) excluding bottomLabel
.嵌入所有questionLabels的在UIStackView
(垂直),不包括bottomLabel
。 Set AutoLayoutConstraint between UIStackView
and bottomLabel
.之间设置AutoLayoutConstraint UIStackView
和bottomLabel
。
Set the numberOfLines
property of UILabel
s to 0(zero).将UILabel
的numberOfLines
属性设置为 0(零)。
Set the Distribution of the UIStackView
as Fill
设置UIStackView
的Distribution为Fill
Then, in your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
method hide the labels.然后,在您的tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
方法中隐藏标签。 And it will automatically handle the spaces between UILabel
s它会自动处理UILabel
之间的空格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.questionLabel1.text = labelOneText[indexPath.row]
cell.questionLabel2.text = labelTwoText[indexPath.row]
cell.questionLabel3.text = labelThreeText[indexPath.row]
if labelOneText[indexPath.row] == "" {
cell.questionLabel1.isHidden = true
}
if labelTwoText[indexPath.row] == "" {
cell.questionLabel2.isHidden = true
}
if labelThreeText[indexPath.row] == "" {
cell.questionLabel3.isHidden = true
}
return cell
}
Final Output:最终输出:
First I suggest you to set UITableViewCell
Height to automatic dimensions
.首先,我建议您将UITableViewCell
Height 设置为automatic dimensions
。 Attach all the children to one another and last child to uiview
of xib .将所有孩子相互连接,最后一个孩子连接到uiview
的 uiview 。 Now hiding view does not adjust size of cell so you need to play with height constraint of uiview
you are hiding .现在隐藏视图不会调整单元格的大小,因此您需要使用您隐藏的uiview
高度约束。
Make height constraint as strong in IBOutlet
else it will crash since cells are re-using and constraint
after setting once will become nil
.在IBOutlet
高度约束,否则它会崩溃,因为单元格正在重用,并且设置一次后的constraint
将变为nil
。 You need to make sure that height constraint are change according to display cell requirement , thats mean for each cell
maintain some datasource
that decide to show or hide the view every time when cellforrowatIndexpath
method called.您需要确保根据显示单元格要求更改高度约束,这意味着每个cell
维护一些datasource
,每次调用cellforrowatIndexpath
方法时决定显示或隐藏视图。
Hope this helps希望这可以帮助
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {
return UITableView.automaticDimension
}
now in cell, put all your views and there siblings which you want to hide/show in UIstackview (horizontal)
.现在在单元格中,将所有视图和要隐藏/显示的兄弟姐妹放在UIstackview (horizontal)
。 now if you hide one view, it will be hidden and its apace will be also hidden to no white space will be showing, and no need to handle extra constraints.现在,如果你隐藏一个视图,它就会被隐藏,它的速度也会被隐藏,不会显示空白,也不需要处理额外的约束。 it will all handled by stackview
.它将全部由stackview
处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.