【问题标题】:second table view is not appearing in scroll view第二个表格视图未出现在滚动视图中
【发布时间】:2015-04-13 03:11:46
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c uitableview uiscrollview scroll


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2013-10-14
      • 2017-05-04
      • 1970-01-01
      相关资源
      最近更新 更多