Search code examples
iosuitableviewios7

Wrong value from UITableView:rowHeight at iOS8


The code I used to create a rectangle (at least until iOS7) was

CGRect rect = [cTableView frame];
rect.origin.y += [cTableView rowHeight];
searchOverlayView = [[BecomeFirstResponderControl alloc] initWithFrame:rect];

On iOS7, cTableView (an instance of a UITableView) returned 44. Testing in iOS8 with an iPhone 5s returns -1.

Why is this happening? What is the correct code that needs to be used in order for my app to be backwards compatible with iOS7?


Solution

  • 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).