0

I have a UITableView that is populated with cells of variable height. I would like the table to scroll to the bottom when the view is pushed into view.

I currently have the following function:

[self.table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

I'm using autolayout, dynamic table cells and UITableViewAutomaticDimension as row height. I set estimatedRowHeight = 100

The above code works fine in viewDidLoad however this has the unfortunate side effect of displaying the top of the table when the view first appears and then jumping to the bottom. I would prefer it if the table view could be scrolled to the bottom before it appears.

Please note: I'm loading data from core data.

Any guidance would be much appreciated, even if it's just a case of telling me what I have is all that is possible.

S.Haris
  • 19
  • 4

2 Answers2

1

Theoretically, the order of the calls is correct: viewDidLoad should be called before the view is rendered, so the only thing that comes to my mind is that probably scrollToRowAtIndexPath:atScrollPosition:animated: is asynchronous. Maybe there's something you can do with UIScrollViewDelegate (remember, UITableView inherits from UIScrollView) - https://developer.apple.com/documentation/uikit/uiscrollviewdelegate?language=objc

Maybe you can do something like hiding the view on onLoad and showing it on scrollViewDidScroll:. I don't have time to test and paste a snippet but... maybe it helps :) Good luck!

Bartserk
  • 675
  • 6
  • 18
  • Thanks for your answer. It works !!!. But in low memory devices, I'm facing the issue again. I don't know why it is happening there only. It works fine in iPhone 8 and above, but issue is there in iPhone 6s. – S.Haris May 27 '19 at 09:00
  • Also in low performance devices, hiding/ showing views is not working properly as user can see the delay. – S.Haris May 27 '19 at 09:04
  • Hehe to be honest, I don't think iPhone 6s qualifies as "low memory", I expected an iPhone 3GS or so :) But if that's your case, make sure the change of visibility is the last line on the `scrollViewDidScroll:` callback. It's the only thing that comes to mind, because the race condition due to asynchronism should be solved. – Bartserk May 29 '19 at 07:00
0

This seems pretty reliable...

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 100;

    [self.tableView performBatchUpdates:nil completion:^(BOOL b) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:numRows - 1 inSection:numSections - 1];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }];

}

Replace numRows and numSections with your data count(s).

DonMag
  • 69,424
  • 5
  • 50
  • 86