iOS:在Swift中动态设置UITableView的高度 [英] iOS: Dynamically set UITableView height in Swift

查看:418
本文介绍了iOS:在Swift中动态设置UITableView的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的UITableView存在于ViewController(而不是TableViewController)上.我们在运行时用两行填充它.当我们设置高度限制时,表格看起来很好.当我们设置高度限制时,表格不会完全出现.如何设置UITableView来动态设置其高度?

Our UITableView exists on a ViewController (and not a TableViewController). We populate it at run time with two rows. When we set a height constraint the table appears fine. When we don't set the height constraint the table completely does not appear. How can you set the UITableView to dynamically set its height?

我看过一些关于动态设置行高(效果很好)但关于实际表高的线程.在这个特定点上有一些线程,但是似乎没有一个对我有用.

I've seen threads about dynamically setting the row height (which does work well) but not about the actual table height. There are a few threads on this specific point but none seem to work for me.

有关解决方案的更新:正如Robotic Cat所建议的那样,我们在情节提要中的UITableView上设置了高度限制(我们已经设置了高度限制),然后按Ctrl拖动以在其中创建IBOutlet我们的控制器.在此,根据Vignesh的答案,在viewWillAppear中执行以下代码:

Update on solution: As Robotic Cat suggested we set up a height constraint on the UITableView in the Storyboard (we had that already) and then did Ctrl drag to create an IBOutlet in our controller. From there we executed the following code in viewWillAppear per Vignesh's answer:

tableViewHeightConstraint.constant = tableView.contentSize.height

请注意,在执行上述代码之前,我们以相同的方法调用reloadData()来填充表格.

Note before executing the above code we are calling reloadData() in the same method to populate our table.

推荐答案

我们没有UITableView的自动尺寸,它们仅影响UITableViewCell的高度.如果要动态设置表格视图高度,我认为最好的方法是在表格视图加载完成后将表格视图高度约束设置为等于其内容大小.我的意思是这样,

We don't have automatic dimension for UITableView, they only affect the UITableViewCell height. If you want to set the table view height dynamically, I think the best way is to set the table view height constraint equal to its content size after the table view done loading. I mean something like,

tableViewHeightConstraint.constant = tableView.contentSize.height

这篇关于iOS:在Swift中动态设置UITableView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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