Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my UITableView fail to scroll unless I tell it to animate the scroll?

I've set up a table view so that it will scroll to a certain row when its view controller loads. I put this code into the viewDidLoad method, and it works how I expect it:

[thisTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:16 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

However, if I change that last bit to animated:NO, it doesn't scroll at all. I'm fine with having it animate, but I'm wondering: why doesn't it scroll if animated is set to NO?

like image 768
Aaron Brown Avatar asked May 01 '12 22:05

Aaron Brown


1 Answers

In viewDidLoad, your tableView is empty, scrolling to row 16 is then not possible. The call is evaluated immediately because no need for animation

When you use animated:YES, the animation is delayed until the view is displayed, your table is populated by then.

You may want to use this kind of code in viewDidAppear (or viewWillAppear) instead, when your table has already been populated with data. This will call it every single time the view is presented though (unless you add some logic), not sure if that meets your requirement...

like image 128
JP Hribovsek Avatar answered Oct 21 '22 02:10

JP Hribovsek