Set TableView height by the number or rows
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.
Eliko
Updated on November 08, 2021Comments
-
Eliko over 2 years
I have got
TableView
in theMainstoryBoard
and the number of rows is random every time. I want the height of the wholeTableView
to be flexible - by that I mean, for example: if I got 4 rows in theTableView
and eachTableView
row height is 22 so theTableView
height will be 88. Another example: number of rows: 2 row height = 22TableView
will be 44.How can I make it?
-
Eliko almost 9 yearsChange the UITableView height. for example if i got 2 rows and each row is 22 height so the UITableView height will be 44
-
Logunath almost 9 yearsIf you want to dynamically change the UITableView height try this, it may help you. stackoverflow.com/questions/18784741/…
-
Eliko almost 9 yearsSohil, 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 almost 9 years@SohilR.Memon Tried it, doesn't work.. (by the way it changed it to override func)
-
Kampai about 8 years@SohilR.Memon: How to increase tableView height using Autolayout?
-
Sohil R. Memon about 8 years@Kampai You want to increase the
UITableView
height orUITableViewCell
height? -
Kampai about 8 yearsI like to change (increase / decrease) height of table base on numbers of rows, but using auto layout.
-
Sohil R. Memon about 8 years@Kampai Please take the outlet of HeightConstrainst of
UITableView
and change it accordingly usingheightConst.constant = number_of_rows * height of each row
-
Kampai about 8 yearsI 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 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 about 8 years@SohilR.Memon: Here is my question - stackoverflow.com/questions/35363292/…
-
Jack almost 7 yearsWorked for me, No need of reload data in
func viewDidLayoutSubviews()
-
Huy-Anh Hoang over 6 yearsWhy do we use
frame
and notbounds
? -
Tom Schulz about 6 yearsThis 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 almost 6 yearsIt 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 almost 5 yearsim confuse... why do you update the frame before loading the data?
-
Ammar Mujeeb about 4 yearsI have to do it in DispatchQueue.main.async block, then it worked . Thanks
-
Muju over 3 yearsHow can I get cell height instead setting it static as 30?
-
Kostarev Kirill almost 3 yearsWhy this is not a best answer? You saved my day!
-
Prabhdeep Singh over 2 yearsThanks man. i set the top,trailing,leading in storyboard and height in code. Worked for me