• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

调整 UITableView 的大小以适应内容

用户头像
it1352
帮助7

问题说明

我正在创建一个应用程序,该应用程序将在 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?

正确答案

#1

其实我自己找到了答案.

Actually I found the answer myself.

我只是用 table.contentSize.heightheighttableView.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;
    });

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangbccke
系列文章
更多 icon
同类精品
更多 icon
继续加载