【问题标题】:One viewcontroller for multiple UITabBar sections用于多个 UITabBar 部分的一个视图控制器
【发布时间】:2009-10-26 16:43:43
【问题描述】:

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

我想为每个视图使用相同的视图控制器,并带有设置数据源和表标题的选项,而不是编写 8 个不同的视图控制器。这可能吗?更重要的是,这怎么可能?

更新
使用 Ole Begemann 发布的代码,我让它在一定程度上起作用。但是,我意识到我最初的问题混淆了一些行话。 UITableViewDataSource 是一个官方协议,对我想要完成的事情做的太多了。我只需要设置表一次的逻辑,填充表视图的数据是唯一不同的东西。我所说的“数据源”只是一个对象字典,可以由我的视图控制器的 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];

如何将这个字典从代理传递给 UITableViewController?

【问题讨论】:

    标签: iphone objective-c uiviewcontroller


    【解决方案1】:

    作为Daniel says,您可以创建任意多个相同视图控制器的实例,然后将这些实例分配给标签栏控制器的标签。

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

    【讨论】:

    • 太棒了!这正是我一直在寻找的。谢谢。
    【解决方案2】:

    当然可以,因为您可以像使用不同的数据源一样多次实例化视图控制器...您将遵循相同的过程,就好像每个选项卡都有不同的视图控制器一样...

    【讨论】:

    • 感谢您的回复。您能否进一步详细说明如何将其付诸实践。一个代码示例会很棒。
    【解决方案3】:

    您可以将 UIToolBar 与您想要的任何按钮一起使用,并在每次单击按钮时使用不同的参数调用 reloadData。

    【讨论】:

    • 这也适用于 UITabBarItem 吗?我不确定标签栏项目是否有这种方法。
    • 不,您应该改用 UIBarButtonItem。您可以为每个项目设置目标和操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 2014-12-06
    相关资源
    最近更新 更多