gpt4 book ai didi

iphone - 多个 UITabBar 部分的一个 View Controller

转载 作者:搜寻专家 更新时间:2023-10-30 20:04:51 25 4
gpt4 key购买 nike

我目前正在开发一个简单的 iPhone 应用程序,使用 UITabBar 和表格 View 来显示数据。标签栏包含 8 个部分,每个部分包含相同的 View 但使用不同的数据源, TableView 的标题也不同,但除此之外 View 是相同的。

我想为每个 View 使用相同的 View Controller ,并提供设置数据源和表格标题的选项,而不是编写 8 个差异很小的单独 View Controller 。这可能吗?更重要的是,这怎么可能?

更新
我使用 Ole Begemann 发布的代码让它在一定程度上工作。但是,我意识到我最初的问题混淆了一些行话。 UITableViewDataSource 是一个官方协议(protocol),对我想要完成的事情来说太多了。我只需要设置一次表格的逻辑,填充表格 View 的数据是唯一不同的东西。我所说的“数据源”只是一个对象字典,我的 View Controller 的 UITableView 函数可以使用这些对象。

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

如何将这个字典从委托(delegate)传递给 UITableViewController?

最佳答案

作为Daniel says ,您可以根据需要创建同一 View Controller 的任意多个实例,然后将这些实例分配给选项卡栏 Controller 的选项卡。

代码可能如下所示(我在这里只创建了 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];

关于iphone - 多个 UITabBar 部分的一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1625849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com