一个用于多个UITabBar节的viewcontroller [英] One viewcontroller for multiple UITabBar sections

查看:45
本文介绍了一个用于多个UITabBar节的viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用UITabBar和用于显示数据的表格视图开发一个简单的iPhone应用程序.该选项卡包含8个部分,每个部分包含相同的视图,但使用不同的数据源,表视图的标题也不同,但不同之处在于视图是相同的.

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.

我想为每个视图使用相同的viewcontroller,并提供用于设置数据源和表标题的选项,而不是编写8个具有最小差异的独立viewcontroller.这是否可能,更重要的是,这怎么可能?

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?

更新
使用Ole Begemann发布的代码,它在一定程度上可以正常工作.但是,我意识到我最初的问题混淆了一些行话. UITableViewDataSource是一个官方协议,对于我要完成的工作而言,它做得太多了.我只需要逻辑一次设置表,填充表视图的数据是唯一不同的东西.我所说的数据源"只是一个对象字典,可以由我的viewcontroller的UITableView函数使用.

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];

如何将字典从委托人传递给UITableViewController?

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

推荐答案

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.

代码看起来像这样(我在这里仅创建了3个实例):

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];

这篇关于一个用于多个UITabBar节的viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆