views:

131

answers:

2

Hi

I am sure this a pretty common problem: I have a UITableView with quite some entries in it. If you click on a cell, you get to see some details about the selected object (done with a navigation controller). Now if the user hits the back arrow at the top of the screen, he successfully returns to the old table, but this is now scrolled all the was back to the top, no matter which cell the user touched before.

I create the table in my "viewWillAppear" method of the ViewController. Yet I think this is not my issue, as log entries have shown that the "reloadData" call (which is in this method) does not get called when I return from another view (hit the back button).

Does anybody know how I can prevent the UITableview from scrolling up when returning from a deeper level?

Thanks a lot!

A: 

One way is you can record the indexpath that the cell was in, then use UITableView scrolLToRowAtIndexPath to scroll to the cell that was selected, heres a ref http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/scrollToRowAtIndexPath:atScrollPosition:animated:

Daniel
+3  A: 

You should create the table view in -[viewDidLoad]. This will most likely not only solve your problem, but is also a cleaner way to do something like this.

Currently, the table view is newly created every time the view appears, which of course also happens when you return to it by using the back button.

mrueg
Thanks a lot, your comment kicked me to check the documentation again and now I saw why loadView previously did not work for me. All good now, thanks! And the problem with the scrolling has also disappeared :) Perfect!
Robin