在一个类中管理两个tableviews

时间:2011-10-20 05:23:55

标签: iphone ios ipad uitableview

如何使用tabledatasource和tabledelegates访问单个类中的两个或多个tableView?

4 个答案:

答案 0 :(得分:4)

在同一个委托方法中,您必须处理所有的表视图,

例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==tableView1)
  //value for first tableview
else if(tableview==tableView2)
  //value for second tableview
}

答案 1 :(得分:1)

如果您要从XIB添加它们,那么KingofBliss的答案是正确的,否则如果您以编程方式添加它们,您可以在它们上设置tag属性,然后在委托中使用它来区分您正在处理哪个表。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView tag] == 1)
{
  //value for first tableview
} else {
  //value for second tableview
}

答案 2 :(得分:1)

TableView tableView = [[UITableView alloc] init]; 当你创建tableview时给它们标记..

tableView1.tag         = 10;
tableView1.delegate    = self;
tableView1.dataSource  = self;
[self.view addSubview:tableView1];
[tableView1 release]; 

UITableView tableView2 = [[UITableView alloc] init];
tableView2.tag         = 20;
tableView2.delegate    = self;
tableView2.dataSource  = self;
[self.view addSubview:tableView2];
[tableView2 release]; 

在代表中

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 if(tableView.tag == tableView1.tag)
   //value for first tableview
 else if(tableview.tag==tableView2.tag)
   //value for second tableview
}

答案 3 :(得分:0)

我认为没有必要添加两个UITableView并区分它们,因为将调用相同的委托函数。只需添加一个UITabaleView并执行检查数据,设置标志,例如,如果当前你的UITableView填充了Array1,现在你想用Array2填充它只需设置标志并调用 [UITableView reloadData]

你的问题解决了。

至于我的建议,在同一个类中添加两个UITableView并不是一个好习惯