Get Height of Table Contents in Swift
23,063
Solution 1
Swift:
var tableViewHeight: CGFloat {
tableView.layoutIfNeeded()
return tableView.contentSize.height
}
Objective-C
- (CGFloat)tableViewHeight {
[tableView layoutIfNeeded];
return [tableView contentSize].height;
}
Solution 2
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
myTbleView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
myTbleView.removeObserver(self, forKeyPath: "contentSize")
super.viewWillDisappear(true)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
let newsize = newvalue as! CGSize
tableViewHeightConstraint.constant = newsize.height
}
}
}
Hope this will help you.
Solution 3
var obs: NSKeyValueObservation?
obs = tableView.observe(\.contentSize, options: .new) { (_, change) in
guard let height = change.newValue?.height else { return }
self.constantHeight.constant = height
}
Related videos on Youtube
Author by
Austin E
Updated on July 31, 2022Comments
-
Austin E almost 2 years
I'm trying to get the height of all of a table's contents so I can update the container it is in. This will allow the container and other views in the scroll view to scroll together.
-
Hamed Nova about 7 yearsPossible duplicate of Set UITableView's height to the height of its content with Auto Layout
-
Hamed Nova about 7 yearssee this i answered the same question.good luck
-
-
vivek bhoraniya about 7 yearsI think setting height based on tableview content height is not good way as its depend on when you are getting table content height...If you call this method right after reloading table then it may be possible that you might get wrong content size
-
Atpl Mohali over 5 yearsThis code is working like a charm, but I have a problem I have two tableview in a same view controller, then this code set the same height for both the tableview's. So is there any other way to get the height of tableview separately.
-
Aman Gupta over 5 yearsif (object as! UITableView) == firstTableView {} else if (object as! UITableView) == secondTableView {}
-
Trevor over 5 yearsThanks! I think it would be best to add the observer in viewWillAppear, if you modally present another view controller or have a tab bar etc, you will remove the observer and not add it on return. The functionality will no longer be there and the app will crash when trying to remove an observer that is not added.
-
Himanshu Moradiya over 5 yearsIts working but i just notify that your observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call that much time execute.
-
Bruno Henrique almost 4 yearsI think creating a custom Notification that fires only when your data is loaded will solve the problem, the method above works but it will call observer many times... it might cause your app to stutter.
-
Biks about 3 yearsThis is a perfect answer. Also works for dynamic cell size.