cocoa touch – Switching between two UITableViewControllers within a single UINavigationController – Education Career Blog

I have a UINavigationController. In its toolbar is a segmented control with two buttons. Each button relates to its own UITableViewController.

What I’m trying to achieve is someway of wiring up the navigation controller so that the views are switched depending on which button within the segmented control is active.

I assume I should hold on to the table controllers, because I want to preserve the scrolling position within each view, e.g., if the user was positioned at the top of table 1, and the bottom of table 2, then this information should be preserved when switching.

Any suggestions would be gratefully received!


Would it be possible to just switch the dataSource instead of having to deal with two separate table views? Preserving the scrolling position can still just as easily be done, and you end up using less memory (one table view and two data sources instead of two table views and two data sources).

With only 128MB to spare, memory efficiency is king on the iPhone.


Joel’s suggestion is a good one. Another possibility is to use two different UITableViews in your view controller, and swap them out manually. There’s nothing special about a UITableViewController; it’s just a UIViewController with its tableView setup for you. Use a standard UIViewController, add two outlets for tables, hide one, and swap them when your UISegmentedControl is toggled.


I’ve used Joel’s method. Very efficient.

Use the segmented control to call a method that sets a sets a variable, then sends tableView reloadData.

Then the tableView: numberOfRowsInSection: and tableView: cellForRowAtIndexPath: retrieve their data depending on the value of the variable set by your segmented control’s method.

Leave a Comment