【问题标题】:How to Add TableView With two Section to UIScrollView as a sub view如何将带有两个部分的 TableView 作为子视图添加到 UIScrollView
【发布时间】:2013-08-08 20:22:50
【问题描述】:

在我的编码中,表格视图会显示,但是当我滚动表格视图时,它会完全滚动。我只滚动了表格视图单元格。请任何人帮助我。这是我的代码。

[super viewDidLoad];
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];
scrollview.showsVerticalScrollIndicator=NO;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,900);
[scrollview addSubview:recipePhoto];
[scrollview addSubview:detailLabel];
[scrollview addSubview:prizeLabel];
[scrollview addSubview:discountLabel];
[scrollview addSubview:saveLabel];
UITableView  *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 600, 400) style:UITableViewStylePlain];
[tableView setAutoresizesSubviews:YES];
[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
tableView.scrollEnabled = NO;
tableView.backgroundColor=[UIColor clearColor];
[tableView setDataSource:self];
[tableView setDelegate:self];
[scrollview addSubview:tableView];
[tableView reloadData];

【问题讨论】:

  • 你能不能说的更清楚一点。有什么问题
  • 你为什么使用 UITableViewStylePlain。如果您需要两个部分,请使用 UITableViewStyleGrouped 而不是 UITableViewStylePlain。
  • 这似乎是这个问题的重复:stackoverflow.com/questions/3422915/…
  • 表格视图已显示,但如果我单击单元格,它看起来像蓝色,如果我单击另一个单元格,它看起来像蓝色,但它不会改变选择所有单元格同时被选中。

标签: iphone ios uiscrollview


【解决方案1】:

我在您提供的代码中没有发现任何问题。您可能想查看其他部分的视图或在此处分享更多信息,以便我为您提供帮助。

我在示例项目中添加了您的代码,并且能够滚动查看表格视图。下面是我使用的代码(几乎和你的一样,注释标签)

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

    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];
    scrollview.showsVerticalScrollIndicator=NO;
    scrollview.scrollEnabled=YES;
    scrollview.userInteractionEnabled=YES;
    [self.view addSubview:scrollview];
    scrollview.backgroundColor = [UIColor grayColor];
    scrollview.contentSize = CGSizeMake(320,900);
//    [scrollview addSubview:recipePhoto];
//    [scrollview addSubview:detailLabel];
//    [scrollview addSubview:prizeLabel];
//    [scrollview addSubview:discountLabel];
//    [scrollview addSubview:saveLabel];
    UITableView  *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 600, 400) style:UITableViewStylePlain];
    [tableView setAutoresizesSubviews:YES];
    [tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    tableView.scrollEnabled = NO;
    tableView.backgroundColor=[UIColor clearColor];
    [tableView setDataSource:self];
    [tableView setDelegate:self];
    [scrollview addSubview:tableView];
    [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"cell %d", indexPath.row];
    cell.backgroundColor = [UIColor blueColor];
    return cell;
}

【讨论】:

    【解决方案2】:

    这里有两种方法来完成该要求:

    1. 只需设置与UITableView 相关的UIScrollViewcontentSize contentSize.height

    只需在上述代码之后添加此代码

    tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
    
    float fscrview = tableView.frame.origin.y + tableView.contentSize.height + 20;
    yourScrollView.contentSize=CGSizeMake(320, fscrview);
    

    还将scrollEnabled = NO; 设置为您在代码中设置的tableView

    2. 当 TableView 滚动时,设置 scrollEnabled = NOUIScrollView

    【讨论】:

    • 但我有很多标签。当我禁用滚动视图时意味着我不显示剩余视图。
    • 好的,然后按照我上面的选项设置 Height 及其内容.. 设置每个表格的高度及其内容,最后最后一个表格只需将 scrollView 的 contentSize 设置为最后一个表格的高度..跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多