Dynamic row heights of a UITableView inside a UITableViewCell

前端 未结 2 523
执念已碎
执念已碎 2021-01-19 03:57

I have a \'master\' UITableView, in which I use different cells that are loaded from .xib\'s. Some of these cells themselves also have a UITableView.

For now I have

2条回答
  •  借酒劲吻你
    2021-01-19 04:56

    First Approach:

    1. Create a subclass for child tableView and override intrinsicContentSize.

           class MyOwnTableView: UITableView {
          override var intrinsicContentSize: CGSize {
              self.layoutIfNeeded()
              return self.contentSize
          }
      
          override var contentSize: CGSize {
              didSet{
                  self.invalidateIntrinsicContentSize()
              }
          }
      
          override func reloadData() {
              super.reloadData()
              self.invalidateIntrinsicContentSize()
          }
      }
      
    2. In Interface builder change the class of your child tableView to MyOwnTableView (subclass UITableView).

    3. Set automatic row height for both the parent and child table view.

          tableView.estimatedRowHeight = 60.0;
          tableView.rowHeight = UITableViewAutomaticDimension;
      

    Second Approach: 1. Create a height constraint with any value for child tableView and conect an IBOutlet that sets the child tableView height. 2. Set the height constraint's constant to tableView.contentSize.height

        self.tableViewHeight.constant = self.tableView.contentSize.height
    

提交回复
热议问题