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
}
Share:
23,063

Related videos on Youtube

Austin E
Author by

Austin E

Updated on July 31, 2022

Comments

  • Austin E
    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.

  • vivek bhoraniya
    vivek bhoraniya about 7 years
    I 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
    Atpl Mohali over 5 years
    This 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
    Aman Gupta over 5 years
    if (object as! UITableView) == firstTableView {} else if (object as! UITableView) == secondTableView {}
  • Trevor
    Trevor over 5 years
    Thanks! 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
    Himanshu Moradiya over 5 years
    Its 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
    Bruno Henrique almost 4 years
    I 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
    Biks about 3 years
    This is a perfect answer. Also works for dynamic cell size.