I have a bit of a problem with my iOS app in xcode. I have a UITableView that loads a few hundred cells. When I scroll down to a specific cell and drill down to detailedviewcontrollers and return again the master view table has returned all the way to the top. I've had a look at the following 2 questions that are similar.
How can I get the UITableView scroll position so I can save it?
Setting scroll position in UITableView
I still can't get these to work. I'm not the most experienced coder so I'm really struggling with this.
I know things like viewWillDisappear and viewDidAppear need to be changed, but I just really can't get much further than that.
On this table I have a reloadData feature so it is possible to pull down the latest data from the server and also a working search bar.
Anyway, a helping hand would be great. Thanks,
Luke
See here you have to first save the scrolled position of
tableView
i.e.contentOffset
oftableView
and then reuse it when you are coming back totableView
.1)When and where you will save it :
When : At the point, when you are drilling down to
detailViewController
save thecontentOffset
oftableView
How :
float verticalContentOffset = tableView.contentOffset.y;
2) How will you set tableView back to the same scrolled position :
Hope this will help you.
Sorted!
With a bit of inspiration from Desdenova, whilst at work I had a good think about it and realised what it could be. Remembering that I had a search bar, I had implemented the following code a few months ago to hide it:
In naivety I put that in
viewDidAppear
rather thanviewDidLoad
. Obviously, this did hide the search bar, but with it being in the wrong void command it did it every time the masterDetailView returned to the top of the stack. So, after moving the above code to viewDidLoad it now still hides it, but only the once.I'm just spelling it out like this for other beginners, like myself, who may come across the same problem and may just save their sanity!
Thanks to you all for your ideas that helped me out.
+1 to you all!
For some reason this was halfway working for me (sometimes the position would only be approximate). I finally used the following a priori less clean solution
which worked better.
My solution to this problem consisted in scrolling the table to the indexrow of the last item.
My decision for a collection:
For the table, the principle is the same.
If anyone is wondering, I tried the solution by Prashant N and it worked. However
reloadData
don't actually reset the scroll position anymore now, so I didn't have to do anything other thanreloadData
.