I want to calculate Content size of my UITableView
. I am using autolayout with height UITableViewAutomaticDimension
.
Tried to get [UITableView contentsize]
after reloading tableview, in that case height get calculated based on estimatedRowHeight
.
self.tableView.estimatedRowHeight = 50; self.tableView.rowHeight = UITableViewAutomaticDimension;
Delegate -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewAutomaticDimension; }
Trying to get content size and assign it to height constrain.
[self.tableView reloadData]; [self.tableView layoutIfNeeded]; self.tableHeightConstraint.constant = self.tableView.contentSize.height;
Any suggestions? Thanks in advance.
Edit:
Finally I am solved my problem with some tweak. I changed tableview height to max (In my case 300 is max) before reloading data on table, so tableview has space to resize all cells.
self.tableHeightConstraint.constant = 300; [self.tableView reloadData]; [self.tableView layoutIfNeeded]; self.tableHeightConstraint.constant = self.tableView.contentSize.height;
Thanks for help.
Finally I am solved my problem with some tweak. I changed tableview height to max (Objective-C: CGFLOAT_MAX
, Swift: CGFloat.greatestFiniteMagnitude
) before reloading data on table, so tableview has space to resize all cells.
Objective-C:
self.tableHeightConstraint.constant = CGFLOAT_MAX; [self.tableView reloadData]; [self.tableView layoutIfNeeded]; self.tableHeightConstraint.constant = self.tableView.contentSize.height;
Swift:
tableHeightConstraint.constant = CGFloat.greatestFiniteMagnitude tableView.reloadData() tableView.layoutIfNeeded() tableHeightConstraint.constant = contentTableView.contentSize.height
Posting this so it will be helpful for others.
Finally, I understood you problem and here is the solution of it.
I hope you have already done this.
UITableView
.IBOutlet
of UITableView
Height.viewDidLayoutSubviews
method you can get the original UITableView
height after populating the data.Update the below code:
override func viewDidLayoutSubviews() { constTableViewHeight.constant = tableView.contentSize.height }
Update:
self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
Please check this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With