具有多个数据源的UITableView

问题描述 投票:0回答:1

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

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

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

请指教。

objective-c uitableview
1个回答
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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.