UITableview内容大小在使用估计高度时出错

时间:2019-01-29 18:39:57

标签: ios swift uitableview

使用表格视图内容大小时,我的空间越来越小。我像 https://forums.developer.apple.com/thread/81895 这样称呼它。但这对我没有用。当我打电话来获取表格内容的大小时,例如,它给出了错误的大小,如果表格视图内容的大小为100,则表示为140或80,例如...等。因此,如果有人知道如何处理或如何获得正确的表视图内容大小??

1 个答案:

答案 0 :(得分:1)

只需使用下面的代码

override func viewDidLoad() {
  super.viewDidLoad()
  yourTableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if(keyPath == "contentSize"){
        if let newvalue = change?[.newKey] {
            let contentHeight: CGFloat = yourTableView.contentSize.height
            print(contentHeight)
        }
    }
}