Eliko
Eliko

Reputation: 1137

Set TableView height by the number or rows

I have got TableView in the MainstoryBoard and the number of rows is random every time. I want the height of the whole TableView to be flexible - by that I mean, for example: if I got 4 rows in the TableView and each TableView row height is 22 so the TableView height will be 88. Another example: number of rows: 2 row height = 22 TableView will be 44.

How can I make it?

Upvotes: 24

Views: 50424

Answers (10)

Super Developer
Super Developer

Reputation: 897

Please find the below code.

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UIScreen.main.bounds.height
}
        
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let tableViewHeight = self.tableView.frame.height
    let contentHeight = self.tableView.contentSize.height
            
    let centeringInset = (tableViewHeight - contentHeight) / 2.0
    let topInset = max(centeringInset, 0.0)
            
    self.tableView.contentInset = UIEdgeInsets(top: topInset, left: 0.0, bottom: 0.0, right: 0.0)
}

Upvotes: 1

Musharaf Islam
Musharaf Islam

Reputation: 41

nothing worked here is the solution i ended up using which gives accurate height.

extension UITableView {
    var contentSizeHeight: CGFloat {
        var height = CGFloat(0)
        for section in 0..<numberOfSections {
            height = height + rectForHeader(inSection: section).height
            let rows = numberOfRows(inSection: section)
            for row in 0..<rows {
                height = height + rectForRow(at: IndexPath(row: row, section: section)).height
            }
        }
        return height
    }
}

Usage:

tableView.contentSizeHeight

will give you the actual calculated height of your table view content.

Upvotes: 4

Bushra Shaikh
Bushra Shaikh

Reputation: 1

Take outlet of tableview height and set it with your tableview row height in cellForRowAt like this,

tableviewHeight.constant = (tableView.contentSize.height * no_of_rows) + bottom_space

Upvotes: 0

user12688502
user12688502

Reputation:

Simply take outlet for tableview height constraint and update it in willDisplay cell: UITableViewCell method of UITableView

@IBOutlet weak var tblHConstraint: NSLayoutConstraint!


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        self.tblHConstraint.constant = self.tblView.contentSize.height
    }

Upvotes: 5

suresh
suresh

Reputation: 53

Note: when your are increasing the tableview height it will goes out side the view. and you will get a problem with scrolling.

Ex: take a view inside that keep tableview and give constraints to left,right,bottom,top to Zero(0).

now reload the tableview assume 2 rows each row height is 20 now total rows height is 40. its fine and gave those height to table view, Table view will show only 40 height (you can assign tableview height by taking constraints (or) table view content size both are give same height according to rows height).

But if you reload 50 rows you will get scrolling problem because those rows height given to table view height, here table view height expands more than your screen height.

To solve this problem you should use scroll view outside the tableview.

Upvotes: 0

joemissamore
joemissamore

Reputation: 69

To use with with autolayout:

Somewhere in viewDidLoad()

tableView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor)

and then in your viewDidLayoutSubViews()

tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true

Upvotes: 3

Naresh
Naresh

Reputation: 17912

DispatchQueue.main.async {
    var frame = tableView.frame
    frame.size.height = tableView.contentSize.height
    tableView.frame = frame
}

OR

DispatchQueue.main.async {
    self.TblViewHeightConstraint.constant = CGFloat((self.array.count) * 30)//Here 30 is my cell height
    self.TblView.reloadData()
}

Upvotes: 2

Sohel L.
Sohel L.

Reputation: 9540

You can change the UITableView height as per the contentSize as below:

Swift 2.2

tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height)

Swift 3 or 4+

tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)

and make sure you write the above line in viewDidAppear method

You need to write the above line in viewDidLayoutSubviews also.

Swift 2.2

func viewDidLayoutSubviews(){
     tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height)
     tableView.reloadData()
}

Swift 3 or 4+

func viewDidLayoutSubviews(){
     tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
     tableView.reloadData()
}

Upvotes: 44

Sreeraj VR
Sreeraj VR

Reputation: 1574

Use this for Swift 3

 override func viewDidLayoutSubviews(){
    tableView.frame = CGRect(x: tableView.frame.origin.x, y:  tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
    tableView.reloadData()
}

Upvotes: 0

Mr. JD Agrawal
Mr. JD Agrawal

Reputation: 144

Take outlet of the Height Constraint of the parent view and assign it the height of table view's content + constant(extra height of other contents in the view)

heightConstraintView.constant = tableView.contentSize.height + constantValue //use the value of constant as required.

and write this code in the cellForRowAt method.

Upvotes: 3

Related Questions