Bottom to top UITableView scrolling to the end of different element heights
I have a UITableView with different element heights. First, the user should see the last items (which are at the end of the list) and should be able to manually scroll up.
To get to the end of the list, I use [tableView scrollToRowAtIndexPath:lastIndexPath atScrollPositionBottom animated:NO]
the main thread after I have called [tableView reloadData]
in the background process. To speed things up and get to the end, I use estimatedHeightForRowAtIndexPath
and I return the first 8 elements (CGFloat)500
for other elements as well (CGFloat)100
. Usage estimatedHeightForRowAtIndexPath
avoids calling heightForRowAtIndexPath
for every item in the list (it can be a very long list).
With iOS 8 (iPhone 4S, 5S, 6) everything works fine, but with iOS 7 (iPhone 4S) only heightForRowAtIndexPath
gets a call but not cellForRowAtIndexPath
, so until scrolling starts, the item won't show. Returning (CGFloat)100
for each estimatedHeightForRowAtIndexPath
-call avoids this, but it doesn't scroll to the real end of the list and doesn't match with the iOS 8 version of the iPhone 4S, but it's pretty slow.
Any suggestions?
source to share
We have completed the best implementation estimatedHeightForRowAtIndexPath
. There, we compute a higher estimate of the height for the first 50 elements using simple calculations, so we get values that are close to the real height. For the rest of the items, we return the average from the first calculation.
source to share
Since you know you want to scroll to the bottom, you can use
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
Then you don't have to calculate the cell heights and add the heights of the section headers and footers.
source to share