UITtackView中没有显示UITableView [英] UITableView not shown inside UIStackView
问题描述
我有一个 UIStackView
,它可以根据用户选择显示标签,图像,表视图或任何内容。这是我当前动态视图的层次结构:
I have a UIStackView
where it can show a label, image, table view or nothing based on user selection. Here is my current hierarchy for the dynamic view:
- UIStackView_Parent
- UILabel - 一些文字,固定视图
- UIStackView_Child - 这是可以显示多件事或什么也没有的容器
- UIView - 另一个固定视图
当我打电话给
UIStackView_Child.addArrangedSubview(...)带有标签或图像的
它可以很好地工作,但addArrangedSubview(tableView)
不会显示该表。我有tableView.scrollEnabled = false
并根据单元格数将帧设置为固定高度,并定义了表的cellHeight
。When I call
UIStackView_Child.addArrangedSubview(...)
with a label or image it works perfectly, butaddArrangedSubview(tableView)
does not show the table. I havetableView.scrollEnabled = false
and set frame to fixed height based on number of cell and defined the table'scellHeight
.非常感谢任何帮助,谢谢!
Any help would be much appreciated, thanks!
推荐答案
那是因为stackview试图尽可能地压缩内容。当你初步添加一个tableview时,我假设它没有内容,所以stackview将宽度压缩为0,因为它没有任何显示。您必须执行以下两项操作之一:
That's because stackview tries to compress content as much as possible. When you initally add a tableview, I'm assuming it has no content, so stackview compresses the width to 0 because it has nothing to show. You have to do one of two things:
在表填充数据后,您必须设置tableView的框架。我通过计算每个细胞的大小来做到这一点。我知道宽度将与包含stackview的视图一样大。所以它最终会像
After the table gets populated with data, you have to set the tableView's frame. I do this by calculating how big each cell is going to be. I know the width is going to be as big as the view that contains stackview. So it winds up being something like
let estimatedHeight = tableView.numberOfRows(inSection: 0) //You may need to modify as necessary let width = parentView.frame.size.width tableView.frame = CGRect(x: 0, y: 0, width: width, height: estimatedHeight)
设置tableView的框架后,stackview应自动调整。
Once you set the tableView's frame, stackview should automatically adjust.
这篇关于UITtackView中没有显示UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!