具有多个数据源的UITableView

时间:2010-10-26 19:03:49

标签: objective-c uitableview

我有一个需要使用2 UITableViews的ViewController 1总是显示,而另一个将在您单击视图上的按钮后显示为弹出窗口。

通常我将委托和datasource设置为文件所有者。但是,由于UITableViews中有1个是弹出窗口,我不知道如何最好地解决这个问题。

例如,我如何处理此部分-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

请建议。

1 个答案:

答案 0 :(得分:7)

您应该在控制器中声明两个表视图的实例变量:

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{ 
  UITableView *mainTableView;
  UITableView *popupTableView;
}

在每个数据源或委托方法中,您可以检查调用者传递的表视图:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(tableView == mainTableView)
  {
    // Code to create and return a main table view cell
  }
  else if(tableView == popupTableView)
  {
    // Code to create and return a popup table view cell
  }
}