I just downloaded Xcode6 Beta6 and found some strange defects in my App. After some initial debugging, I have found that heightForRowAtIndexPath
is called numerous extra times and is causing some ill effects.
Normally, I would expect heightForRowAtIndexPath
to be called 2 extra times than the rows I am displaying. So when I hardcode numberOfRowsInSection
to return 3, I should be seeing heightForRowAtIndexPath
get called 5 times. In iOS 7 that is the case, but when I test in Xcode6 Beta6 I am seeing heightForRowAtIndexPath
get called 7 times. When I return 4, the method gets called 9, 5 and its 11. So it seems the pattern is twice the number of rows I am hardcoding + 1.
Has anyone else encountered this problem or found a way to solve it? Just being able to confirm that this is an iOS bug would be a huge help because then I can defer the defect for later as Apple may fix this problem on their own.
Edit: This issue still exists in Xcode 6 GM
Edit 2: Apple responded and closed my bug report, here is there response:
The API contract of a delegate is that we can call this method as much as we want.
I guess we just have to deal with this and should code to not rely on exactly when this delegate is called.
iOS 8 onwards, Apple has changed the way they decide the content size of a table view. This is to optimize it further and enable us to have more dynamism in the table design.
As part of this, they now no longer call heightForRowAtIndex path for all the rows in one shot, instead they just call it as many times as they want in order to determine the content size. The content size determination has also been made kind of dynamic, hence the multiple calls.
Don't do any height calculations inside the heightForRowAtIndexPath method. Instead write a private method and treat it as the heightForRowAtIndexPath of the past. Store all the heights in a dictionary against an NSIndexPath object as key.
Of course, this would mean to call this private method before calling reloadData, if the data has changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With