Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 Auto height cell not correct height at first load

Tags:

I have an iOS 8 application where I have a search results page that each cell is autosized based on the height of a certain label. However, after the view first loads the cells that are displayed don't autosize. After you scroll down the cells that follow are sized correctly. I am wiring up the auto layout constraints in my storyboard. I have the following code in viewDidLoad.

- (void)viewDidLoad {     [super viewDidLoad];      //configure tableView so that we use autolayout enabled row heights     self.tableView.estimatedRowHeight = 68;     self.tableView.rowHeight = UITableViewAutomaticDimension; } 

Has anyone else experienced this?

like image 392
Dennis Avatar asked Jan 05 '15 20:01

Dennis


2 Answers

Yes, I've seen this same problem when making the views and constraints in the storyboard (but not with code added views). Iv'e fixed this by adding this code in the custom cell class,

-(void)didMoveToSuperview {      [self layoutIfNeeded]; } 

This could probably go other places, but this method seems to be called only once, so I thought it was a good place to do it.

like image 96
rdelmar Avatar answered Oct 20 '22 05:10

rdelmar


The answer from rdelmar did not work for me.

What I have done is added this code:

[tableView setNeedsLayout]; [tableView layoutIfNeeded]; [tableView reloadData]; 

after data is fetched and first [tableView reloadData] is called, which in my case is located in viewDidLoad method of my View Controller.

So result code would be:

NSArray *fetchedData = ...  [tableView reloadData]; //Fist table reload [tableView setNeedsLayout]; [tableView layoutIfNeeded]; [tableView reloadData]; //Second table reload 

This answer is taken from here: http://useyourloaf.com/blog/self-sizing-table-view-cells.html#comment-1783719287

like image 23
Sihad Begovic Avatar answered Oct 20 '22 05:10

Sihad Begovic