0
我正在创建一个tableview,当单元格数量增加或减少时,它会在高度上动态变化。为了完成这一点,我添加了一个观测到我的tableview:TableView的观察值
tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
我收到此用下面的方法:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
tableView.reloadData()
}
然而,当我运行此我得到一个错误:
Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeed414ff8)
我相信这些行导致该错误的,并已试图打电话super.observeValue(...)
任何帮助将不胜感激。
你为什么这样做? tableviews height是屏幕上容器的高度,其contentSize.height是正在滚动的内容的高度。当这两个值相等时,表格将不会滚动。 – Losiowaty