【发布时间】:2021-12-03 05:52:12
【问题描述】:
我正在为我的 Swift 项目使用 tableView.addObserver 和 observeValue 方法来根据其内容提供 tableView 高度。它工作得很好,但我不知道怎么做。有人可以用这个解释一下吗 这是我的代码。
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
tableView.layer.removeAllAnimations()
heightTable.constant = tableView.contentSize.height
UIView.animate(withDuration: 0.5) {
self.updateViewConstraints()
}
}
【问题讨论】:
标签: ios swift key-value-observing