简体   繁体   English

如何根据其内容高度设置 UITableView 高度?

[英]How to set UITableView height according to its content height?

I have a question about UITableView .我有一个关于UITableView的问题。
I want to let the tableView height according to my cells content height.我想根据我的单元格内容高度让 tableView 高度。
So, I use the following code successfully.所以,我成功地使用了以下代码。

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

But when I have much data to show, my contents will out of screen.但是当我有很多数据要显示时,我的内容会超出屏幕。
And the tableView also out of screen.并且 tableView 也在屏幕外。
Have any ideas to set it's constrain and don't make it out of screen.有任何想法来设置它的约束并且不要让它超出屏幕。
I want to set 15 between the tableView bottom layout and superView bottom layout.我想在tableView底部布局和superView底部布局之间设置15。
I use SnapKit to set autolayout.我使用 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)
} 

You could create a custom UITableView :您可以创建一个自定义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)
  }

}

And then set your UITableView Class to AutomaticHeightTableView .然后将您的UITableView类设置为AutomaticHeightTableView

This solution is inspired from an answer found on stackoverflow.该解决方案的灵感来自于在 stackoverflow 上找到的答案。

I have solved a similar problem using the following method.我已经使用以下方法解决了类似的问题。 You only need few lines of code.您只需要几行代码。

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

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

Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.只需为 UITableView 设置一个估计的行高,然后将 rowHeight 设置为 UITableViewAutomaticDimension。

Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:也许你可以限制表格框架的高度,确保不超过它的超级视图,像这样修改你的代码:

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
    }
}

Code Work :代码工作:

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!
    }


}

Constraints to tableView :对 tableView 的约束:

在此处输入图片说明

Output :输出 :

在此处输入图片说明

在此处输入图片说明

Put your UITableView inside a UIScrollView and add constraints like this:将您的 UITableView 放在 UIScrollView 中并添加如下约束:

Constraints to Table View表视图的约束

在此处输入图片说明

Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController创建高度约束的 IBOutlet,然后在 UIViewController 中覆盖viewWillLayoutSubviews

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过其内容动态设置UIScrollView和UITableView的高度 - Dynamically set UIScrollView and UITableView height by its content 使用自动布局将 UITableView 的高度设置为其内容的高度 - Set UITableView's height to the height of its content with Auto Layout iOS-如何使UICollectionViewCell根据其内容调整其高度? (包含一个UITableView) - iOS - How to make an UICollectionViewCell adapt its height according to its content ? (containing an UITableView) 如何根据动态高度单元格设置UITableView的高度限制? - How to set height constraint of UITableView according to dynamic height cells? UITableview Height根据单元格设置 - UITableview Height set according to cells 如何根据子视图的高度设置堆栈视图的高度? - How to set stack view height according to its subviews height? 如何根据UITableViewCells的高度来调整UITableView的高度? - How to resize UITableView height according to the height of UITableViewCells? 如何根据iOS 8中的内容动态调整UITableview标头高度 - How to adjust the UITableview Header height dynamically according to the content in ios 8 如何根据其内容更新TableView及其父ScrollView Height - How to Update TableView and its parent ScrollView Height according to its content 根据文本内容的高度设置单元格的高度 - Set the height of the cell according to the height of the text content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM