首页 > 解决方案 > 使用估计高度时 UITableview 内容大小出错

问题描述

我在使用Table view content size时得到了空白空间。我将其称为https://forums.developer.apple.com/thread/81895。但这对我不起作用。当我调用获取表格内容大小时,它给出了错误的大小,例如,如果表格视图内容大小为 100 意味着它给了我 140 或 80 之类的......等等。那么,如果有人知道如何处理或如何获得正确的表格视图内容大小?

标签: iosswiftuitableview

解决方案


使用以下代码一次

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)
        }
    }
}

推荐阅读