Search code examples
iosuitableviewios7ios8autolayout

Dynamic cell height in iOS 7 and iOS 8 - can I only support heightForRowAtIndexPath in iOS 7?


I have a table view with dynamically sized cells. This works great in iOS 8 but it's not supported in iOS 7. I'm required to implement tableView:heightForRowAtIndexPath or the app will crash in iOS 7. If I do that, I'm going to have to calculate the height of the cell. The problem is that if I implement this method, iOS 8 pays attention to it and no longer does its dynamically sized cell magic. Is there any way to only implement this method for iOS 7 clients?


Solution

  • The new internal autolayout self-sizing cells from iOS 8 cannot magically be reflected back to iOS 7. But that's not a problem. If you want to be backwards-compatible to iOS 7, then, as you rightly say, this means that you will determine the cell height yourself, so that you can return the correct value from tableView:heightForRowAtIndexPath:. This is what we did in iOS 4, 5, 6, and 7. That will still work in iOS 8 too (and is probably faster anyway).

    So my advice would be: just write the code as if for iOS 7. If you're coding in Objective-C you could have two different sets of code (conditional compilation), and even in Swift you could use two different classes, depending on what system we find ourselves on; but I regard that as a bit over the top - it's a code maintenance nightmare waiting to happen.