Search code examples
swiftuitableviewheightconstraints

Swift height of content of table view


Is there a way to find out the height of the content of a table view, beside adding up the height of all cells from all sections?

I have 3 sections each with different height cells and variable number of cells. So I am hoping for a system method to get the height of the content of the table view.

EDIT: nowhere in the answer of the other similar question does it say where to call the tableView.contentSize


Solution

  • You can retreive the content size by :

    Swift / ObjC :

    let contentSize : CGSize = self.tableView.contentSize
    let width = self.tableView.contentSize.width
    let height = self.tableView.contentSize.height
    

    You can use this to know if your UITableView is loaded (Swift 2.2):

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == (tableView.indexPathsForVisibleRows!.last! as! NSIndexPath).row {
            // End of loading
            let contentSize : CGSize = self.tableView.contentSize
            let width = self.tableView.contentSize.width
            let height = self.tableView.contentSize.height
        }
    }