iOS 8 Uitableview First Row Has Wrong Height

iOS 8 UITableView first row has wrong height

I had a problem where the cells' height were not correct on the first load, but after scrolling up-and-down the cells' height were fixed.

I tried all of the different 'fixes' for this problem and then eventually found that calling these functions after initially calling self.tableView.reloadData.

            self.tableView.reloadData()
// Bug in 8.0+ where need to call the following three methods in order to get the tableView to correctly size the tableViewCells on the initial load.
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.reloadData()

Only do these extra layout calls after the initial load.

I found this very helpful information here: https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/10

Update:
Sometimes you might have to also completely configure your cell in heightForRowAtIndexPath and then return the calculated cell height. Check out this link for a good example of that, http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout , specifically the part on heightForRowAtIndexPath.

Update 2: I've also found it VERY beneficial to override estimatedHeightForRowAtIndexPath and supply somewhat accurate row height estimates. This is very helpful if you have a UITableView with cells that can be all kinds of different heights.

Here's a contrived sample implementation of estimatedHeightForRowAtIndexPath:

public override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell

switch cell.type {
case .Small:
return kSmallHeight
case .Medium:
return kMediumHeight
case .Large:
return kLargeHeight
default:
break
}
return UITableViewAutomaticDimension
}

Update 3: UITableViewAutomaticDimension has been fixed for iOS 9 (woo-hoo!). So you're cells should automatically size themselves without having to calculate the cells height manually.

Wrong value from UITableView:rowHeight at iOS8

Apple changed the default row height in iOS8 to UITableViewAutomaticDimension, which is declared as -1. This means that your table view is set up for automatic cell height calculation.

You will either need to implement autoLayout (recommended) or implement the new delegate method: heightForRowAtIndexPath. Here's a great question about auto layout: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Seems like you were effectively hard coding 44 (the old default) anyway, though, so you could just do that (not recommended).

iOS 8 Auto height cell not correct height at first load

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.

UITableView dynamic cell heights only correct after some scrolling

I don't know this is clearly documented or not, but adding [cell layoutIfNeeded] before returning cell solves your problem.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
NSUInteger n1 = firstLabelWordCount[indexPath.row];
NSUInteger n2 = secondLabelWordCount[indexPath.row];
[cell setNumberOfWordsForFirstLabel:n1 secondLabel:n2];

[cell layoutIfNeeded]; // <- added

return cell;
}

iOS 8 self-sizing cells - wrongly calculated height for labels and Infinite scrolling

There is a label in your cell which has preferred width being set, remove the setting seems to fix the problem.

Since you have already make the label width to be equal with the cell width, there is no need to set the Preferred Width.

And try to remove these code.

self.tableView.estimatedRowHeight = 600;
tableView.reloadData()

which seems to be redundant here.



Related Topics



Leave a reply



Submit