One view manager for multiple UITabBar sections

I am currently developing a simple iPhone application using UITabBar and table views to display data. The tab contains 8 sections, each of which contains the same view, but using a different data source, the name of the table view is also different, but, in addition, the views are the same.

I would like to use the same viewcontroller for each view with parameters to set the data source and the table title instead of writing 8 separate view controllers with minimal differences. It would be possible and, more importantly, how is this possible?

Refresh
This works for me to a certain extent, using the code sent by Ole Begemann. However, I realized that my initial question was mixed up with some kind of jargon. UITableViewDataSource is an official protocol and does too much for what I'm trying to accomplish. I only need the logic to create tables once, the data to populate the table view is the only thing that is different. What I meant by "datasource" was just a dictionary of objects that could be used by my UITableView functions of my view manager.

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 to pass this dictionary from delegate to UITableViewController?

+3
source share
3 answers

, , , .

( 3 ):

// 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];
+4

, , , ... , , ...

+1

UIToolBar , , reloadData .

0

Source: https://habr.com/ru/post/1721103/


All Articles