如何将UITableView的高度设置为其内容高度?

时间:2018-11-23 07:33:15

标签: ios swift uitableview

我对UITableView有疑问。
我想根据单元格内容的高度让tableView的高度。
因此,我成功使用了以下代码。

DispatchQueue.main.async {
    var frame = self.tableView.frame
    frame.size.height = self.tableView.contentSize.height
    self.tableView.frame = frame
}

但是当我有很多数据要显示时,我的内容将不在屏幕上。
并且tableView也超出屏幕。
有什么想法可以设置约束,不要让它超出屏幕。
我想在tableView底部布局和superView底部布局之间设置15。
我使用SnapKit设置自动布局。

//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.

tableView.snp.makeConstraints { (make) in 
    make.top.equalTo(self.topLayoutGuide.snp.bottom)
    make.left.equalTo(10)
    make.right.equalTo(-10)
    make.bottom.equalTo(-15)
} 

6 个答案:

答案 0 :(得分:1)

您可以创建自定义UITableView

class AutomaticHeightTableView: UITableView {

  override var contentSize: CGSize {
    didSet {
      self.invalidateIntrinsicContentSize()
    }
  }

  override var intrinsicContentSize: CGSize {
    self.layoutIfNeeded()
    return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
  }

}

然后将您的UITableView类设置为AutomaticHeightTableView

此解决方案的灵感来自stackoverflow上的答案。

答案 1 :(得分:0)

您尝试过

self.tableView.invalidateIntrinsicContentSize()

https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize

答案 2 :(得分:0)

也许您可以限制表格框架的高度,确保其长度不超过其superView,类似这样的代码修改方式:

DispatchQueue.main.async {
    if let superViewHeight = self.tableView.superView?.bounds.maxY {
        let maxHeight = superViewHeight - self.tableView.frame.minY
        var frame = self.tableView.frame
        frame.size.height = min(self.tableView.contentSize.height, maxHeight)
        self.tableView.frame = frame
    }
}

答案 3 :(得分:0)

代码工作:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
    @IBOutlet weak var tblView: UITableView!

    var tblMaxHeight : CGFloat = 50

    override func viewDidLoad() {
        super.viewDidLoad()

        let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height

        tblMaxHeight = self.view.frame.size.height - 40 - navHeight

    }


    override func viewDidLayoutSubviews(){

        tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 24
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        cell?.textLabel?.text = "Row: #\(indexPath.row)"

        return cell!
    }


}

对tableView的约束:

enter image description here

输出:

enter image description here

enter image description here

答案 4 :(得分:0)

将UITableView放在UIScrollView内,并添加如下约束:

对表格视图的约束

enter image description here

创建一个具有高度限制的IBOutlet,然后在您的UIViewController中覆盖 viewWillLayoutSubviews

override func viewWillLayoutSubviews() {
    super.updateViewConstraints()
    self.tableViewHeightConstraint.constant = tableView.contentSize.height
}

答案 5 :(得分:0)

我已经使用以下方法解决了类似的问题。您只需要几行代码。

override func viewDidLoad() {
    super.viewDidLoad()
    setupTableView()
}

private func setupTableView() {
    tableView.estimatedRowHeight = 44.0
    tableView.rowHeight = UITableViewAutomaticDimension
}

只需为UITableView设置估计的行高,然后将rowHeight设置为UITableViewAutomaticDimension。

相关问题