【问题标题】:How to set the UITableHeight according to the JSON contents?如何根据 JSON 内容设置 UITableHeight?
【发布时间】:2018-01-10 04:14:55
【问题描述】:

我对 Swift 和整体开发还比较陌生。所以请善待我。 我有一个 UITableView 显示来自 Facebook 的 JSON 数据。它包含用户管理的页面列表。我已经注意显示数据,但我无法根据表格内容调整表格大小。 我之所以问,是因为我还没有通过其他类似的问题找到解决我的问题的方法。

【问题讨论】:

  • 你想改变tableview的高度吗?
  • 使用自己大小的tableView

标签: ios json swift uitableview


【解决方案1】:

我的表格视图

@IBOutlet weak var filterTableView: UITableView!

当你重新加载 TableView 来显示数据时调用UITableView_Auto_Height Func 正如我在 DidLoad 中调用的那样

override func viewDidLoad() 
{
        super.viewDidLoad()    
        self.filterTableView.tableFooterView = UIView()
        filterTableView.delegate = self
        filterTableView.dataSource = self
        filterTableView.isScrollEnabled = false

        //here I called the function 
        UITableView_Auto_Height()
        //Reloading TableView
        filterTableView.reloadData()
    }

所需功能

func UITableView_Auto_Height()
    {
        //Check for the content Size 
        if(self.filterTableView.contentSize.height < self.filterTableView.frame.height)
            {

            //if ContentSize is big set the frame 
            var frame: CGRect = self.filterTableView.frame;
            frame.size.height = self.filterTableView.contentSize.height;

            //Assign frame
            self.filterTableView.frame = frame;
        }
    }

在 TableViews 中重新加载数据时调用该必需函数

对于单元格自动尺寸,您可以在 DidLoad 中只写这两行

self.filterTableView.estimatedRowHeight = 44 // for assumption
self.filterTableView.rowHeight = UITableViewAutomaticDimension

【讨论】:

  • 其实我之前在类似的帖子上看到过你的回答;我试过了,但不幸的是它没有用。
  • UITableView_Auto_Height 即使这对您没有帮助?请您详细说明您获得的确切输出以及您正在寻找的输出以及您的一些用户界面
【解决方案2】:

要设置自己调整大小的单元格,只需将 AutoLayout 添加到单元格,指定 tableView 的估计行高度,并将 tableView 的 rowHeight 设置为 UITableViewAutomaticDimension(这将是未来 XCode6 版本中的默认设置)。

self.tableView.estimatedRowHeight = 49 // estimate height
self.tableView.rowHeight = UITableViewAutomaticDimension

【讨论】:

    【解决方案3】:

    您需要为表格视图使用 UITableViewAutomaticDimension,以便它根据内容处理单元格高度。

    但为了使其正常工作您需要对表格视图单元格使用适当的约束

    自动尺寸会根据它从上到下的约束来计算你的单元格的高度,以便它可以得到预期的高度。

    确保您已将约束正确应用于单元格子视图。

    参考见thisthis

    【讨论】:

    • 您的答案似乎是关于 tableviewcell 但我需要帮助的是整个表格视图。 tableview 有一个固定的高度,所以当内容超过 table 的大小时,用户将不得不滚动而不是 tableview 根据内容调整自身大小。
    • @AsminGhale 我认为您不清楚 TableView 的概念。 tableView 单元格将被重用,它们的动态大小不会增加 tableView 的大小。 tableView 高度将保持不变,您可以滚动行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2013-06-15
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    相关资源
    最近更新 更多