MD1948
MD1948

Reputation: 292

Bottom-up UITableView with different item height scroll to end

I have a UITableView with different item height. At the beginning the user should see the last items (that are at the end of the list) and should be able to scroll manually to the top.

To get to the end of the list I use [tableView scrollToRowAtIndexPath:lastIndexPath atScrollPositionBottom animated:NO] on the main thread after I called [tableView reloadData] on a background process. To speed it up and to get to the real end I use estimatedHeightForRowAtIndexPath and I return for the first 8 items (CGFloat)500 and for the other items (CGFloat)100. Using estimatedHeightForRowAtIndexPath avoids the call for heightForRowAtIndexPath for every item in the list (it could be a very long list).

With iOS 8 (iPhone 4S, 5S, 6) everything works fine, but with iOS 7 (iPhone 4S) just heightForRowAtIndexPath get called but not cellForRowAtIndexPath, so there is no item displayed until I start to scroll. Returning (CGFloat)100 for every estimatedHeightForRowAtIndexPath-call avoids this, but it doesn't scroll to the real end of the list and compaired to the iOS 8 iPhone 4S version it is pretty slow.

Any suggestions?

Upvotes: 3

Views: 503

Answers (3)

Walt Sellers
Walt Sellers

Reputation: 3939

Since you know you want to scroll to the bottom, you could use

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

Then you don't need to calculate cell heights and add heights of section headers and footers.

Upvotes: -1

MD1948
MD1948

Reputation: 292

We ended up with a better implementation of estimatedHeightForRowAtIndexPath. There we calculate with some simple calculations a better estimated height for the first 50 items, so we get values pretty close to the real height. For the other items we return an average from the first calculations.

Upvotes: 0

Lefteris
Lefteris

Reputation: 14677

Well you could simply reload the visible cells after you scroll down....

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] 
                 withRowAnimation:UITableViewRowAnimationNone];

Upvotes: 2

Related Questions