Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 995
cateof Avatar asked Aug 26 '14 15:08

cateof


2 Answers

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

like image 185
Rog Avatar answered Oct 22 '22 07:10

Rog


This made me struggle for hours. I ended up hard coding the value to 44:

self.tableView.rowHeight = 44;
like image 28
Js Lim Avatar answered Oct 22 '22 08:10

Js Lim