Sam
Sam

Reputation: 61

How to set height constraint of UITableView according to dynamic height cells?

I have TableView and I set an initial constraint to it as 10.0, for example. Now, I've added 3 cells with the different height sizes(they are autolayout cells), for example, they sizes are (50.0, 120.0, 30.0)

How can I set now my UITableView.heightConstraint = 50 + 120 + 30?

How can I take autolayout cell size? When I try to print their size I get initial size(standard 44.0)

Any ideas?

Upvotes: 3

Views: 6078

Answers (2)

user3306145
user3306145

Reputation: 76

You can calculate tableviewcell height dynamically.. Try this...

CGfloat height= [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+2;

Upvotes: 0

avismara
avismara

Reputation: 5149

How about this:

self.tableView.reloadData()
self.tableView.layoutIfNeeded() 
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height

Upvotes: 11

Related Questions