views:

377

answers:

1

Good Morning,

I'm hoping someone would be able to shed some light on how i can switch from a ViewController nested in a tab bar controller to another ViewController nested in a navigation controller nested in the tab bar controller, and then scroll the table view in the section navigation controller to a specific section.

I have found at least what i need to switch the tab bar controllers with

[self.applicationTabBarController setSelectedIndex:1];

and i am able to get a reference to the second view controller with

UINavigationController *secondTabNavController = (UINavigationController *)[[self.applicationTabBarController viewControllers] objectAtIndex:1];

MyViewController *myViewController = (MyViewController *)[[ordersNav viewControllers] objectAtIndex:0];

But what i am unclear about is how i can scroll the UITableView to the specific section. I tried

NSIndexPath *myPath = [NSIndexPath indexPathForRow:0 inSection:3];

[myViewController.ordersTableView scrollToRowAtIndexPath:myPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

Assuming of course, that the second view has been loaded, and has a section 3.

And lastly, my first view controller calls the above code that resides in the app delegate.

Thank you.

A: 

You need to be careful where you put that scrollToRowAtIndexPath call. If you put it before the view loads, it may be running that method on nil. I would say have a function in myViewController called scrollToRowAtIndexPath: which determines if the view is currently displayed, and if it is, just calls the tableView version. If not, save the indexPath until viewWillAppear: is called and run it then.

Ed Marty
Ed, Thanks for your reply.I was able to get the tableview to scroll to its position when the tableview has been loaded with data at least once.However, im a little stumped on the second part. if the tableview had never been loaded i defer the scroll to position until a later time, in this case viewWillAppear. It seems that in viewWillAppear and viewDidAppear the table has not been loaded yet so it could not scroll to the positon. Is there an event that fires when the table view is finished asking for data?Thanks,
Chris1Shop
In response to my own comment, I had noticed that on viewWillAppear, the tableview was not loaded and could scroll to position. Howver, in that method, if i call reloadData on the table, the tableview will scroll to the correct index.Im not convinced that this is the proper way to achieve my goal. but it worked.
Chris1Shop