Resizing UITableView to fit content(调整 UITableView 的大小以适应内容)
问题描述
我正在创建一个应用程序,该应用程序将在 UILabel
中有一个问题,并在 UITableView
中显示一个多项选择答案,每行显示一个多项选择.问题和答案会有所不同,所以我需要这个 UITableView
的高度是动态的.
I am creating an app which will have a question in a UILabel
and a multiple choice answers displayed in UITableView
, each row showing a multiple choice. Questions and answers will vary, so I need this UITableView
to be dynamic in height.
我想为该表找到一个 sizeToFit
解决方法.表格的框架设置为其所有内容的高度.
I would like to find a sizeToFit
work around for the table. Where the table's frame is set to the height of all it's content.
谁能建议我如何实现这一目标?
Can anyone advise on how I can achieve this?
推荐答案
其实我自己找到了答案.
Actually I found the answer myself.
我只是用 table.contentSize.height
的 height
为 tableView.frame
创建一个新的 CGRect
>
I just create a new CGRect
for the tableView.frame
with the height
of table.contentSize.height
这会将 UITableView
的高度设置为其内容的 height
.由于代码修改了UI,别忘了在主线程中运行:
That sets the height of the UITableView
to the height
of its content.
Since the code modifies the UI, do not forget to run it in the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
//This code will run in the main thread:
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
});
这篇关于调整 UITableView 的大小以适应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!