0

As the question asks I would like to know how to make multiple table view controllers on the same view controller. SOrry about the formatting but in the end I added a segmented control change function that will set which table views are hidden and which appear. Hence I would like to know how to separate and build my table views to do the following. Or if you can tell me how to change the data that goes into the table view with selecting a different option on the segmented controller, that would help as well. Thanks

@implementation SecondViewController{
NSDictionary *beerContents;
NSArray *beerNames;
NSDictionary *wineContents;
NSArray *wineNames;

}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


    NSURL *beerUrl = [[NSBundle mainBundle] URLForResource:@"BEER2"       withExtension:@"plist"];
beerContents = [NSDictionary dictionaryWithContentsOfURL:beerUrl];
beerNames = beerContents.allKeys;


NSURL *wineUrl = [[NSBundle mainBundle] URLForResource:@"Wine" withExtension:@"plist"];
wineContents = [NSDictionary dictionaryWithContentsOfURL:wineUrl];
wineNames = wineContents.allKeys;

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [beerNames count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath{

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.textLabel.text = beerNames[indexPath.row];

    return cell;
}


- (IBAction)segmentChanged:(id)sender {
if (_drinkChoice.selectedSegmentIndex == 0) {
    [_beerTableVIew setHidden: NO];
}
if (_drinkChoice.selectedSegmentIndex ==1) {
    [_beerTableVIew setHidden:YES];
}

} @end

2 Answers 2

0

A table view is just a view. Nothing wrong with having more than one. Each one needs a data source and a delegate; these can be different objects, or they can be the same object but in that case clearly every data source and delegate will need to look at the tableView: parameter to tell the different table views apart and supply the appropriate response.

For example:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // return an answer that depends somehow on what `tableView` is
}

On the other hand... it might be argued that instead of a segmented control and multiple table views, a UITabBarController and several UITableViewControllers is a built-in way to do this.

2
  • I get what you're saying, but I already have a tabbed application and this is supposed to be one of the tabbed views already. Based on what you wrote initially, how would I refer to one vs the other Apr 13, 2014 at 1:55
  • I would use subclasses of UITableView. Now you can identify the table view by its class.
    – matt
    Apr 13, 2014 at 3:11
0

If I understand what you are trying to accomplish, I think the best approach is to just use a single UITableView and reload its data when the segmented control changes.

- (IBAction)segmentChanged:(id)sender {
    [self.tableView reloadData];
}

The tableView dataSource will refresh and in your various dataSource and delegate methods use this paradigm:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // return an answer based upon the active section
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.