views:

198

answers:

4

I've Created a UITableViewController subclass. Do I only need one controller? Do I just instantiate multiple instances of that one subclass?

The iPhone App I'm building will start with a Table of a list of people. If the user taps a person, a new table will be pushed in with a list of Companies they've worked for. If the user then taps a company, they'll see a list of Job Positions. And if they tap a position they'll see a list of people holding those positions. This could go on forever, and the user could always back up the list.

The App Delegate instantiates the Navigation Controller and the Table View Controller and then pushes it onto the Navigation Controller. But when a user taps a row, now the TVC is creating another TVC.

  1. Is that right or should the AppDelegate be instantiating all TVC's? Or does it work out since they all get pushed onto the Nav Controller anyway?

  2. Does each Table View instance need to have a different name or can they all be called 'mainTVC' or something like that?

    tableViewController *mainTVC = [[tableViewController alloc] init];
    
  3. Won't I run out of memory? Do i need to start dropping Table Views when they're 2 or 3 levels away from current, and then re-create it if the user backs up to it?

+2  A: 

No need to create multiple TableView's, what I've done in the past is simply re-bind to a different datasource. So keep one TableView and have a datasource for people, then companies, etc...

TSG
Thank You. But can I do this and still animate-in that new data by sliding it in from the right?
Andrew
What you could do is start with only one tableView. When the user navigates to a new dataSource, simply replace the table's datasource - BUT instead of reloading with [myTableView reloadData], use (for every section):[aTableView reloadSections:[NSIndexSet indexSetWithIndex:0/* The section */] withRowAnimation:UITableViewRowAnimationRight];
pop850
+1  A: 

Option number (2) looks good. You can push quite a lot of view controllers onto the stack before memory becomes an issue. The system will clean up most of the heavyweight memory hogs (ie, the views) in the didReceiveMemoryWarning: method. If you're creating a lot of in-memory structures, you may want to clean them up in that method (be sure to call the inherited method).

Ben Gottlieb
+1  A: 

I'd create a view controller for each type. Presumably you'll want to have special display characteristics like a custom tableview cell to display job positions slightly differently then you would people names.

Other then that, @Ben Gottlieb's answer should work quite well. Use lots of view controllers and handle the didReceiveMemoryWarning: method.

One more thing, if the user drills down so far that you want to say they'll never go all the way back (sort of like having an undo stack) you can use the setViewControllers:animated: UINavigationController method to reset the stack to a certain size (say 15 to implement an 'undo buffer' of 15). With this method you can make sure that the first view controller is always your root view controller and the rest are all drilldown instances.

Epsilon Prime
+1  A: 

To answer your third question, as long as you don't have huge data stores in memory, memory shouldn't be an issue. You SHOULD NOT under any circumstances "drop" tableviews - this would lead to crashes(and there's no way to do non-FILO additions/removals to the navigation stack anyway). Under memory pressure, you should only free "nonessential" items like caches. However, this shouldn't be an issue.

Also, if you have more than 3 or so levels, chances are you need to rethink your UI. If users are drilling down 10 levels, it will be tedious to navigate the stack back.

Mike