A view with a table gets pushed onto the screen and I want it to scroll to a certain row in the table before the screen actually displays. I use this code within the final viewcontroller.
NSIndexPath *scrollToPath = [NSIndexPath indexPathForRow:5 inSection:0];
[theTable scrollToRowAtIndexPath:scrollToPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
When I put it in viewDiDAppear method, then it briefly flashes from the intial position of the table (at the top) to the row I want. I don't want it to show the initial position. If I put it in viewDidLoad or viewWillAppear then it crashes with a NSRangeException, presumably because the table isn't set up yet.
How would I get it to scroll without showing the initial position?
Calling
scrollToRowAtIndexPath:
inviewWillAppear:
does not work for me becausetableView
is not loaded yet. But I could solve this with callingscrollToRowAtIndexPath:
after some delay.Hope it will help someone.
You can use GCD to dispatch the scroll into the next iteration of main run loop in viewDidLoad to achieve this behavior. The scroll will be performed before the table view is showed on screen, so there will be no flashing.
in iOS 8 and above
[self.tableView reloadData]
inviewWillAppear
beforescrollToRowAtIndexPath
does not work, you have to do reload inviewDidAppear
as given below:Note: you have to use
UITableViewScrollPositionTop
, otherwise the scrolling will not be as expected and may scroll to the bottom and even to blank areas after the last row if you specifyscrollToRow
as last row.I think in iOS 8 and above the first time the tableView is loaded it does not load all the data used to construct the table, that is the reason you have to use
[tableView reload]
which I think is a bit weird.I just finished wrestling with this. Mine was adding a search bar to the top of the list, initially tucked under the top... ala some of the core apps. I was actually going to ask this same question!
I fear to offer this up, as it seems those who offer things up get pounded down.. but... I was surprised that there was not an easy solution... so I ended up doing this:
I use ABTableViewCell (you can google it) for custom drawn cells (nice and fast!), and when I get called to DRAW the second row (you could do this in your customly drawn cells without ABTableViewCell), I set it there, with a single fire semaphore:
(choose your proper row/section, as suits you... if they were in the same section, you'd probably be setting row to something other than 0)
If you hate my solution (as I can't comment yet), please do me the favor of just leaving it at zero & letting a better solution come to the top.
Oh, also, there is an entry about hiding your search at the top... but mine was already done as a custom cell... here is that link.
enjoy
Thanks to Shaggy and Dying Cactus for pointing me in the right direction. The answer is to load the table and scroll in viewWillAppear:
i had the exact same problem, after trying everything, this worked, the key is if you're using autolayout , you must write scrollToBottom code in viewDidLayoutSubviews
initialize scrollToBottom to true and then do this
doing this will ensure you're almost at the bottom of you're tableView but might not be at the very bottom as its impossible to know the exact bottom offset when you're at the top of the tableView, so after that we can implement scrollViewDidScroll