第二个表视图未出现在滚动视图中

时间:2015-02-12 11:40:16

标签: ios objective-c uitableview uiscrollview scroll

我在滚动视图中有两个表视图(table,table2)。我想从左到右滚动表格视图。在下面的代码中出现第一个表视图(表),但是当我从右向左滑动时,第二个表视图(table2)没有出现在那里。出现空白屏幕。请帮我编码。

- (void)viewDidLoad
{
[super viewDidLoad];


  UIScrollview * theScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
theScrollView.contentSize = CGSizeMake(320*2, self.theScrollView.frame.size.height);
theScrollView.delegate = self;
theScrollView.bounces = YES;
theScrollView.showsVerticalScrollIndicator = YES;
theScrollView.showsHorizontalScrollIndicator = YES;
[self.view addSubview:theScrollView];

 UITableView *  table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table.contentSize=CGSizeMake(320,200);
[table setDataSource:self];
[table setDataSource:self];
[table setDelegate:self];
[table setTranslatesAutoresizingMaskIntoConstraints: YES];
table.tag = 100;
[theScrollView addSubview:self.table];

 UITableView * table2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  table2.contentSize=CGSizeMake(320,200);
[table2 setDataSource:self];
[table2 setDelegate:self];
table2.tag = 101;
[theScrollView addSubview:self.table2];
}
- (void)viewDidUnload
{
[super viewDidUnload];


self.theScrollView = nil;
self.table = nil;
self.table2 = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

if (tableView.tag == 100) {
    return 3;
}
if (tableView.tag == 101) {
    return 4;
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (tableView.tag == 100) {
    return 5;
}
if (tableView.tag == 101) {
    return 5;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if (tableView.tag == 100) {
    return [NSString stringWithFormat:@"%@", @"Plain"];
}
if (tableView.tag == 101) {
    return [NSString stringWithFormat:@"%@", @"Group"];
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView.tag == 100) {
    static NSString *cellIdentifier1 = @"cellIdentifier1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier1] ;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }
    cell.textLabel.text = @"45";
    return cell;
}

if (tableView.tag == 101) {
    static NSString *cellIdentifier2 = @"cellIdentifier2";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier2] ;
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = @"45";;

    return cell;
}

return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

float x = scrollView.contentOffset.x;

if (x > 320/2 && x < 640/2) {
    self.title = @"TableView Group";
}

if (x > 0 && x < 320/2) {
    self.title = @"TableView Plain";
    [self.table reloadData];
}
}

1 个答案:

答案 0 :(得分:2)

根据您的代码,您为两个表视图设置了相同的框架:

CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)

因此,通过将它们添加到滚动视图中,您只需将它们放在彼此的顶部。

尝试更改第二个表格视图的框架:

CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)