2

How can I initialize a UITableView so that it starts off showing something (of my choosing) in the middle of the table, rather than what is at the top? I want to have a certain cell in the table always start at the top of the screen, so you could scroll up or down from there.

Aaron Brown
  • 407
  • 4
  • 17

1 Answers1

4

You can scroll to a certain cell by sending - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated to the table view.

As you have described you want the cell of your choosing to appear "in the middle of the table", so you should use UITableViewScrollPositionMiddle for the scrollPosition argument.

If you do this as soon right after your table is initialized or as soon as your view loads with the animated argument set to NO then the table view will appear with your cell in the middle, just as you've described.


You also write: "I want to have a certain cell in the table always start at the top of the screen".

By instead choosing that cell and the scrollPosition UITableViewScrollPositionTop you can make that cell be at the top of your table view.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • thanks, this works for scrolling the view to where I want it. I want the table view to load in with a certain cell in the middle as well. I tried putting this in viewDidLoad but that didn't accopmlish anything, it still loads with the table starting at the top. Do you know how to deal with this? – Aaron Brown Apr 25 '12 at 21:16
  • perhaps I'm doing this wrong, as I put it in the viewDidLoad of the containing view, not of the table view itself. – Aaron Brown Apr 25 '12 at 21:19
  • See [this answer](http://stackoverflow.com/a/2934445/526433) - you need to reloadData and scroll in your viewWillAppear. – Ertebolle Apr 27 '12 at 06:13