Set TableView height by the number or rows

44,354

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
44,354
Eliko
Author by

Eliko

Updated on November 08, 2021

Comments

  • Eliko
    Eliko over 2 years

    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?

  • Eliko
    Eliko almost 9 years
    Change the UITableView height. for example if i got 2 rows and each row is 22 height so the UITableView height will be 44
  • Logunath
    Logunath almost 9 years
    If you want to dynamically change the UITableView height try this, it may help you. stackoverflow.com/questions/18784741/…
  • Eliko
    Eliko almost 9 years
    Sohil, Thank you, it works. but I got another problem - my TableView is in ScrollView so everytime i move the screen (not the tableView) up and down the height of the tableView changes to its default and not to the contentSize.
  • Eliko
    Eliko almost 9 years
    @SohilR.Memon Tried it, doesn't work.. (by the way it changed it to override func)
  • Kampai
    Kampai about 8 years
    @SohilR.Memon: How to increase tableView height using Autolayout?
  • Sohil R. Memon
    Sohil R. Memon about 8 years
    @Kampai You want to increase the UITableView height or UITableViewCell height?
  • Kampai
    Kampai about 8 years
    I like to change (increase / decrease) height of table base on numbers of rows, but using auto layout.
  • Sohil R. Memon
    Sohil R. Memon about 8 years
    @Kampai Please take the outlet of HeightConstrainst of UITableView and change it accordingly using heightConst.constant = number_of_rows * height of each row
  • Kampai
    Kampai about 8 years
    I already did these but there are warnings of unsatisfied constraints. I get the desire height but still constraints not set properly. AnyWay I will post a new question about this scenario soon.
  • Sohil R. Memon
    Sohil R. Memon about 8 years
    @Kampai Sure no issue, ping me I will send you the demo of it! Or you can upload your demo Project and I can customize according to your requirement
  • Kampai
    Kampai about 8 years
    @SohilR.Memon: Here is my question - stackoverflow.com/questions/35363292/…
  • Jack
    Jack almost 7 years
    Worked for me, No need of reload data in func viewDidLayoutSubviews()
  • Huy-Anh Hoang
    Huy-Anh Hoang over 6 years
    Why do we use frame and not bounds?
  • Tom Schulz
    Tom Schulz about 6 years
    This seems like a better answer to me Mr. Memon's. Any time I am setting a frame in modern iOS I feel like I am doing something wrong. Maybe cellForRow is not the best place for it - certainly it's somewhere that would be reliable called though.
  • Slavcho
    Slavcho almost 6 years
    It works, but if the tableview has 20 cells, there are all initialized at a time, there is no lazy loading... how to achieve this?
  • João Serra
    João Serra almost 5 years
    im confuse... why do you update the frame before loading the data?
  • Ammar Mujeeb
    Ammar Mujeeb about 4 years
    I have to do it in DispatchQueue.main.async block, then it worked . Thanks
  • Muju
    Muju over 3 years
    How can I get cell height instead setting it static as 30?
  • Kostarev Kirill
    Kostarev Kirill almost 3 years
    Why this is not a best answer? You saved my day!
  • Prabhdeep Singh
    Prabhdeep Singh over 2 years
    Thanks man. i set the top,trailing,leading in storyboard and height in code. Worked for me