首先,给定tableview的静态高度,取这个高度出口。下一步是,您需要 tableview 观察来获取高度,然后在这个观察者中分配您可以分配每个 tableview 的高度。
override func viewWillAppear(_ animated: Bool) {
Tblvw1.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
Tblvw2.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
Tblvw3.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
Tblvw4.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Tblvw1.removeObserver(self, forKeyPath: "contentSize")
Tblvw2.removeObserver(self, forKeyPath: "contentSize")
Tblvw3.removeObserver(self, forKeyPath: "contentSize")
Tblvw4.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if object is UITableView {
print("contentSize:= \(Tblvw1.contentSize.height)")
self.heightOfTable1.constant = Tblvw1.contentSize.height
self.heightOfTable2.constant = Tblvw2.contentSize.height
self.heightOfTable3.constant = Tblvw3.contentSize.height
self.heightOfTable4.constant = Tblvw4.contentSize.height
}
}
注意:确保您必须禁用所有tableview的滚动