views:

411

answers:

3

I'm currently developing a simple iPhone application using a UITabBar and table views for displaying data. The tabbar contains 8 sections, each containing the same view but using a different data source, the title of the table view is also different, but apart from that the views are the same.

I'd like to use the same viewcontroller for each view, with options to set the datasource and table title, instead of writing 8 separate viewcontrollers with minimal differences. Would that be possible and, more importantly, how would this be possible?

Update
I have it working to a certain extent, using the code posted by Ole Begemann. However, I realised that my initial question mixed up some jargon. UITableViewDataSource is an official protocol and does too much for what I'm trying to accomplish. I only need the logic to set up tables once, the data to populate the table view is the only thing that's different. What I meant by "datasource" was just an dictionary of objects which could be used by my the UITableView functions of my viewcontroller.

NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"Id", 
                    @"The Name", @"Name", 
                    @"Post", @"Type",
                    @"09-09-2009", @"Meta",
                    @"This is the excerpt, @"Excerpt",
                    @"This is the body text", @"Body",
                    @"icon.jpg", @"Icon", 
                    @"image.jpg", @"Image", nil];

NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];

How can I pass this dictionary from the delegate to the UITableViewController?

A: 

Sure its possible, for one you can instantiate the view controller as many times as ud like with different data sources...You would follow the same procedure that you would as if you had diffrent view controllers for each tab...

Daniel
Thanks for the reply. Could you please elaborate a bit further on how this could be put into practice. A code example would be great.
mensch
A: 

You can use UIToolBar with any buttons you want and call reloadData on each button click with different parameters.

Morion
Would this work on a UITabBarItem as well? I'm not sure if a tabbar item has this method.
mensch
No, you should use UIBarButtonItem instead. You can set target and action for each item.
Morion
+1  A: 

As Daniel says, you can create as many instances of the same view controller as you like and then assign these instances to the tabs of your tab bar controller.

The code could look like this (I only create 3 instances here):

// Create an array of dictionaries that holds the configuration for the table view controllers.
// Assuming tableXDatasource are existing objects that conform to the UITableViewDataSource protocol.
NSArray *tableControllersConfig = [NSArray arrayWithObjects:
    [NSDictionary dictionaryWithObjectsAndKeys:table1Datasource, @"datasource", @"Table 1", @"title", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:table2Datasource, @"datasource", @"Table 2", @"title", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:table3Datasource, @"datasource", @"Table 3", @"title", nil],
    nil];

// Create the table view controller instances and store them in an array
NSMutableArray *tableControllers = [NSMutableArray array];
for (NSDictionary *configDict in tableControllersConfig) {
    // Assuming MyTableViewController is our custom table view controller class
    MyTableViewController *controller = [[MyTableViewController alloc] initWithNibName:@"MyTableViewController" bundle:nil];
    controller.tableView.delegate = controller;
    controller.tableView.dataSource = [configDict valueForKey:@"datasource"];
    controller.title = [configDict valueForKey:@"title"];
    [tableControllers addObject:controller];
    [controller release];
}

// Assign the array of table view controllers to the tab bar controller
self.tabBarController.viewControllers = [NSArray arrayWithArray:tableControllers];
Ole Begemann
Great! This is exactly what I was looking for. Thanks.
mensch