asheyla
asheyla

Reputation: 3325

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

Upvotes: 2

Views: 16258

Answers (4)

Priyank Patel
Priyank Patel

Reputation: 870

Swift 4,5

1.Set tableview height outlet

enter image description here

2.Now add IBOutlet of height Constraint like below code.

@IBOutlet weak var tableviewHeightConstraints: NSLayoutConstraint!

3.Now reload tableview code in viewWillAppear method.

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableview.reloadData()
    }

4.Now using get the tableview content size and set into willDisplayCell as tableview height.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) as! myTableViewCell
        return cell
    }

     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

     //Content wise tableview height
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        tableviewHeightConstraints.constant = tblData.contentSize.height
    }

Upvotes: 2

Ankit Panchotiya
Ankit Panchotiya

Reputation: 351

  1. Set tableview height outlet.
  2. Now using get the tableview content size and set into willDisplayCell as tableview height.
  3. Don't forget if label data is more then 1 line then set lable line is 0.

    override func viewDidLoad() { super.viewDidLoad() //Reload Tableview tblData.reloadData() }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) as! myTableViewCell
        return cell
    }
    
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
     //Content wise tableview height
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
         constTblDataHeight.constant = tblData.contentSize
    }
    

Upvotes: -1

Jimmy James
Jimmy James

Reputation: 847

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

Upvotes: 8

SNarula
SNarula

Reputation: 558

Use tableview's content size property to get the height of content in it:

tableView.layoutIfNeeded
let tblSize = tbl_Data.contentSize
print(tbl_Data.contentSize.height)

Upvotes: 2

Related Questions