开发者

UITableView with multiple datasource

开发者 https://www.devze.com 2023-01-21 07:09 出处:网络
I have a ViewController that needs to use 2 UITableViews. 1 is always showin开发者_如何学Gog, while the other will show up as a popup after you click on a button on the view.

I have a ViewController that needs to use 2 UITableViews.

1 is always showin开发者_如何学Gog, while the other will show up as a popup after you click on a button on the view.

Typically I set the delegate and datasource to the File's Owner. However, since 1 of the UITableViews is in a popup, I'm not sure how to best tackle this.

e.g how do I tackle this part -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Please advice.


You should have instance variables for both table views declared in your controller:

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

In each data source or delegate method, you can check which table view is being passed by the caller:

- (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
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消