如何将多个表视图添加到视图控制器

时间:2014-04-13 01:18:35

标签: uitableview uisegmentedcontrol

问题是我想知道如何在同一个视图控制器上创建多个表视图控制器。关于格式化的问题,但最后我添加了一个分段控件更改函数,该函数将设置隐藏哪些表视图以及哪些表视图出现。因此,我想知道如何分离和构建我的表视图以执行以下操作。或者,如果您可以通过在分段控制器上选择其他选项来告诉我如何更改进入表视图的数据,这也会有所帮助。感谢

@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 个答案:

答案 0 :(得分:0)

表视图只是一个视图。拥有多个人没有错。每个人都需要一个数据源和一个代表;这些可以是不同的对象,或者它们可以是同一个对象,但在这种情况下,显然每个数据源和委托都需要查看tableView:参数,以区分不同的表视图并提供适当的响应。

例如:

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

另一方面......可能有人认为,不是分段控件和多个表视图,而是UITabBarController和几个UITableViewControllers是内置的方法。

答案 1 :(得分:0)

如果我理解您要完成的任务,我认为最好的方法是在分段控件更改时使用单个UITableView并重新加载其数据。

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

tableView dataSource将刷新,并在各种dataSource和delegate方法中使用此范例:

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