views:

326

answers:

1

Once my UITableView has been populated with data, I want to scroll to a specific index. I think this is how you do it:

[[self tableView] scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:3] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

The problem is when I'm supposed to run this. How do you know when a table has finished loading data?

+1  A: 

First of all, you should use indexPathForRow:inSection: for index paths in table views.

If you want to do something after the table view is loaded, use your view controller's viewDidAppear:, and if this is too early, enqueue a selector using performSelector:withObject:afterDelay:.

Adam Woś
I did try viewDidLoad before, but that was too early. I'm going to try viewDidAppear. I'd rather not use timers since that is very hackish. Is there really no event or something that gets dispatched once all the items have been fetched from the data source?
quano
Not really. The items are not *all* fetched - AFAIK your data source/delegate get queried for height of all items, and then for cells for only the visible rows, in order to save memory and processor resources. There's one more way - if you use `reloadData`, you can be certain that after the method call returns, all updates have been carried out, including drawing the cells etc. Maybe this will help.
Adam Woś
I tried viewDidAppear and that seems to work.
quano