简体   繁体   中英

second table view is not appearing in scroll view

I am having two table views (table, table2) in scroll view. I want to scroll the table view left to right and right to left. in this below code 1st table view (table) appears, but when I swipe right to left 2nd table view (table2) is not appearing there. blank screen appears there. please help me in coding.

- (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];
}
}

According to your code, you setting equal frame for both table views:

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

So by adding them to scroll view, you just placing them on top of each other.

Try to change the frame for second table view:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM