26

This morning I just installed new Xcode which includes iOS 6.

I have a table view loaded with a plist file containing chapters and lines. Chapters define the sections.

The user selects chapter and line and the tableview is automatically scrolled to the correct position (in the viewDidLoad).

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView scrollToRowAtIndexPath:indexPath 
    atScrollPosition:UITableViewScrollPositionTop animated:YES];

this works just great in iOS5 simulator.

Trying this in the iOS 6 simulator the scroll is not performed. I get no errors. I have checked, linePos and chapterPos receive correct values but the scroll is not performed.

Any ideas why ?

1
  • 12
    just to add to pain and misery its broken again 5 years later in iOS 11.
    – Sam B
    Sep 27, 2017 at 0:03

10 Answers 10

70

Objective-C:

[self.tableView reloadData];

dispatch_async(dispatch_get_main_queue(), ^{
        NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
        [self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
});

Swift:

tableView.reloadData()

DispatchQueue.main.async {
    let indexPath = IndexPath(row: linePos, section: chapterPos)
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
6
  • It's an important fix, without this the app could crash accidentally, depending on the reloadData processes.
    – BootMaker
    Oct 26, 2015 at 9:14
  • 6
    this is the only thing that works for me (iOS 9.1). The scrollToRowAtIndexPath always failed to scroll to very bottom cell unless I wrapped it in dispatch_async(dispatch_get_main_queue(), { ... } even though scrollToRowAtIndexPath was already being called from main thread.
    – kas-kad
    Dec 7, 2015 at 11:58
  • This is probably the right answer, PLUS put this in the viewDidAppear: Dec 22, 2015 at 15:01
  • 1
    It worked for me in viewWillAppear and without reloading the tableview first. Thanks!
    – Tuslareb
    Mar 17, 2016 at 9:42
  • Simply amazing! I lost almost a day with this problem. Thank you for sharing! Feb 2, 2018 at 18:11
30

For recent versions of iOS, please read Fyodor Volchyok's answer. Note that it's not marked as the accepted answer simply because at the time the question was first asked (Sept. 2012), the current answer was the working solution. More recent versions of iOS also got the same problem which is now solved by Fyodor Volchyok's answer, so you should +1 his answer at that moment.


I found the answer. I have to first reload the data in the tableview

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];

[self.tableView reloadData];

[self.tableView scrollToRowAtIndexPath:indexPath 
    atScrollPosition:UITableViewScrollPositionTop animated:YES];

Even though I found the answer I don't know why it is working in iOS5 and not in iOS6.

EDIT

Perhaps I should add that even though it was working, I was still having a problem in displaying the last row and posted a question for that

UItableview scrollToRowAtIndexPath not displaying last row correctly

As @Raj also asked for it, I should say that I was triggering that in the viewDidLoad. To correct the problem of the last row not displaying correctly I had to put it in the viewDidAppear.

2
  • Thanks for this info, I just ran into the same thing. I also noticed that it does not animate the table scrolling, just displays it at the selected row.
    – John T
    Sep 22, 2012 at 23:07
  • Are you triggering the scrollToRowAtIndexPath call in your -viewDidLoad method? Oct 16, 2012 at 11:40
4

This works in iOS 12 and 13:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true) 
}
3

I ran into another issue (probably a bug) with scrollToRowAtIndexPath specifically on an iPhone X running ios11. My table has a few hundred sections and in collapsed mode ~10 would fit in the visible screen. As the indexPath got deeper, the scrolling gradually fell behind.

For example, when I wanted the search to find the item in row 30, the ScrollPositionTop would have one additional row before the actual row I expect to be at the top.

And as I tested searching for deeper rows, it started falling behind even more where for say anything past 100 rows deep or so, the expected row did not even come in the visible area.

The workaround I found so far is to say animated:NO for the scrolling within dispatch_async, then it works without any glitches.

3

I'm adding this answer as an addition to Fyodor Volchyok's answer. I also found that dispatching solves the issue. I was able to find a workaround that doesn't dispatch.

self.tableView.reloadData()
let index = // the desired index path

// For some reason, requesting the cell prior to 
// scrolling was enough to workaround the issue.
self.tableView.cellForRowAtIndexPath(index)

self.tableView.scrollToRowAtIndexPath(index, atScrollPosition: .Top, animated: false)
1
  • I had a problem with multithreading: when I use dispatch, one of my methods is starting to call twice. And now it works like magic!!! :-) Apr 11, 2017 at 9:48
2

After iOS7 the property automaticallyAdjustsScrollViewInsets of UIViewController default is YES. It will cause system to adjust the contentOffset of tableView when the view controller pushed. Even you call [self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; in the viewWillAppear. The contentOffset also will be changed by system after viewWillAppear. So my solution is:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;

    /// any other codes
}

- (void)viewWillLayoutSubviews {
    self.tableView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, 0, 0);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // change tableView data source

    [self.tableView reloadData];
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.dataSourceArray count] - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
2

One more possible workaround is to call layoutIfNeeded before calling scrollToRowAtIndexPath.

[self.view layoutIfNeeded];
[self.tableView scrollToRowAtIndexPath...];
1

It worked for me in ios11

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0

dispatch_async(dispatch_get_main_queue(), ^{
   NSInteger numberOfSections = self.tableView.numberOfSections;
   if (numberOfSections > 0)
   {
     NSInteger lastSection = numberOfSections - 1;
     NSInteger lastRowInLastSections = [self.tableView numberOfRowsInSection:lastSection] - 1;

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRowInLastSections inSection:lastSection];
     [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:isAnimated];
}
});
0

Fyodor Volchyok's answer in Swift:

tableView.reloadData()
let indexPath = NSIndexPath(forRow: linePos, inSection: chapterPos)
// make sure the scroll is done after data reload was finished
dispatch_async(dispatch_get_main_queue()) {
   self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
0
[self.tableView beginUpdates];
...
[self.tableView endUpdates];

This works for me.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.