在两个TableView之间传递数据

时间:2013-01-24 17:03:12

标签: ios objective-c uitableview tableview

我有一个 ViewController ,其中包含两个 TableViews ,如下所示。

enter image description here

所以顶级表(firstTable)目前有两条记录。我想根据顶部的选择用不同的数据集(data2)填充底部表(secondTable),事实上,这种情况发生在我的许多Windows应用程序中。

最初,我想知道如何将一个viewcontroller作为数据源托管两个表? vikingosegundo 解释了topic中的内容。太好了,我已经得到了那部分照顾。现在,我有来自 Manage1ViewController 的以下代码。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// From Manage1ViewController.m for firstTable
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

NSString *name0 = cell.textLabel.text;
NSString *tsecs0 = [self findTsecs:name0];

[self openCreateDB2];
NSMutableArray *items2;
items2 = [[NSMutableArray alloc] init];
NSString *sql = [NSString stringWithFormat:@"Select * From data2 WHERE tsec = '%i'", [tsecs0 integerValue]];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(connection2, [sql UTF8String], -1, &statement, Nil)==SQLITE_OK){
    while (sqlite3_step(statement)==SQLITE_ROW) {
        char *field1 = (char *) sqlite3_column_text(statement, 1);
        NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
        NSString *str1 = [[NSString alloc]initWithFormat:@"%@", field1Str];
        [items2 addObject:str1];
    }
    sqlite3_finalize(statement);
    sqlite3_close(connection2);
}

/* Start populating secondTable with items2

*/
}

到目前为止,这么好......应用程序根据用户在 firstTable 上的选择需要填充到 secondTable 中的数据(items2)。但是如何?

在这里和那里阅读了几十个主题之后,我了解到有几种方法可以传递数据。好吧,在这种情况下,我不能使用 segue ,因为我在同一个视图控件中传递数据。无论如何,似乎有些人建议我使用代表 NSNotification 来执行此操作。看起来在我的案例中使用委托是合适的。再怎么样?使用弹出窗口,我认为我是否使用了代理人,这是一样的想法吗?

感谢您的建议。

3 个答案:

答案 0 :(得分:3)

您不想使用委托或通知 - 它们还用于将信息传递给控制器​​外的其他对象。您需要实现第一个表的tableView:didSelectRowAtIndexPath:方法以获取用户选择。在该方法中,您将获得第二个表所需的数据(基于选择)并在第二个表上调用reloadData。

答案 1 :(得分:2)

在第一个表格视图中选择一行后调用[self.tableView2 reloadData]。此外,您必须在此方法之外存储所需的数据,以填充第二个UITableView

答案 2 :(得分:0)

我有管理两个表视图的ManageViewController。

// ManageViewController.h //

#import <UIKit/UIKit.h>
#import "Manage1ViewController.h"
#import "Manage2ViewController.h"

@interface ManageViewController : UIViewController {
Manage1ViewController *firstController;
Manage2ViewController *secondController;
IBOutlet UITableView *firstTable;
IBOutlet UITableView *secondTable;
}
@end

// ManageViewController.m //

#import "ManageViewController.h"

@implementation ManageViewController

- (void)viewDidLoad {
[super viewDidLoad];
if (firstController == nil) {
    firstController = [[Manage1ViewController alloc] init];     
}
if (secondController == nil) {
    secondController = [[Manage2ViewController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];

[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
}

@end