Why can't my UITableView scroll if I don't tell it to bring the scroll back to life?

I set up a table view so that it scrolls to a specific line when the controller boots up. I put this code in the viewDidLoad method, and it works the way I expect:

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

However, if I changed this last bit to animated:NO , it doesn't scroll at all. I'm fine with the animation, but I wonder: why doesn't it scroll if the animated one is set to NO?

+6
source share
1 answer

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

When you use animated: YES, the animation is delayed until a view appears, your table will be populated then.

Instead, you can use this kind of code in viewDidAppear (or viewWillAppear) when your table is already populated with data. This will trigger it every time a view is presented though (unless you add some logic), not sure if this meets your requirement ...

+7
source

Source: https://habr.com/ru/post/914679/


All Articles