在一个视图上使用不同数据填充多个表的最佳方法是什么?

时间:2013-09-13 12:12:17

标签: ios xcode ipad

我正在构建 iPad 应用。我想让其中一个视图(xib)显示在运行时填充到每个表中的不同数据的几个表,即,当视图加载时我想为每个表运行查询并将查询结果作为行返回到相关表格。对于tableA,我将运行queryA并返回datasetA,tableB,queryB,datasetB等。我怎样才能做到这一点?你能给出示例代码吗?我还希望能够与表进行交互,例如,选择一行并将信息传递到另一个屏幕。

2 个答案:

答案 0 :(得分:0)

只需创建四个表并将它们放在视图上,将tableview委托和数据源设置为self.Give每个表都有一个特定的标记。在datasource或delegate方法中,在执行操作之前,请检查该特定tableview的标记并运行与其相关的查询。 例如:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
if (tableView.tag ==12) {
    return @"comments:";
}
else
{
return sectiontitle;
}
}

其中一个表有tag = 12,如果标签是12,则执行与该表相关的操作,否则操作与其他表有关。

答案 1 :(得分:0)

比BalaChandra的答案更简单(这是正确的):

我认为你的UITableViews是实例变量(甚至是属性)。因此,如果您已实例化您的表视图,如:

UITableView *tableViewA = [[UITableView alloc] init...];
UITableView *tableViewB = [[UITableView alloc] init...];

例如,您不需要将标记设置为tableviews。 在UITableViewDelegate / UITableViewDataSource方法中,您可以检查哪个tableView具有以下内容:

if (tableView == tableViewA) {
    // Do something with tableA... 
}
else if (tableView == tableViewB) {
    // Do something with tableB...
}
...