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
Sorting Nsarray Containing Nsdate Objects
How to Create a Popup Menu in iOS
How to Create a Scnnode from a .Usdz
iOS Where to Put Custom Cell Design? Awakefromnib or Cellforrowatindexpath
Uicollectionview - Horizontal Paging with One Cell at a Time
Calculate Controlpoints While Drawing in iOS
Swift Unrecognized Selector Sent to Instance - What Am I Missing
Error | [Ios] File Patterns: the 'Source_Files' Pattern Did Not Match Any File
What Is The Purpose of Launch Images in an iOS Application
Import Framework for Whole Project at One Place in Swift
How to Go to Specific Native View Controller from React-Native Code
Xcode 8.3 Swift Version Error (Swift_Version) in Objective C Project
iOS - Running Background Task for Update User Location Using Swift
Load Custom Error HTMLstring When Wkwebview Loadrequest Fails