Dynamic UITableView height

后端 未结 11 1661
無奈伤痛
無奈伤痛 2020-12-29 09:47

I would like to set the UITableView to match the height for all the contents in the table view.

This is my storyboard

The problem with this is the t

11条回答
  •  忘掉有多难
    2020-12-29 10:08

    You need to set an IBOutlet to the NSLayoutConstraint that sets the tableView height (first you need create the height constraint with any value, doesn't matter) and then ctrl drag it to your class file

    Then in your viewWillAppear you have to calculate the tableView height and set it. Like this:

    var tableViewHeight:CGFloat = 0;
    for (var i = tableView(self.tableView , numberOfRowsInSection: 0) - 1; i>0; i-=1 ){
        tableViewHeight = height + tableView(self.tableView, heightForRowAtIndexPath: NSIndexPath(forRow: i, inSection: 0) )
    }
    tableViewHeightLayout.constant = tableViewHeight
    

    And that's pretty much it. That will give your scrollView content size and shouldn't raise any warnings.

提交回复
热议问题