添加多个子视图时滚动视图不滚动

时间:2015-03-12 18:44:41

标签: ios objective-c uitableview uiscrollview subview

我正在以编程方式添加UIScrollView添加子视图,但是当子视图离开屏幕时,滚动视图不会滚动。任何想法?

我需要多次更新scrollview的内容大小。

首先,我将scrollView本身添加为视图的子视图,如下所示:

    [super viewDidLoad];

    [self addScrollView];
   [self APISimpleDemo];

}

#pragma mark Add Scroll View
-(void)addScrollView {

    //Add Scroll View
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
    CGRect scrollFrame;
    scrollFrame.size.height = self.view.frame.size.height - (navHeight - 150); //set height minus nav bar and top section
    scrollFrame.size.width = self.view.frame.size.width;
    scrollFrame.origin.y = 180;
    scrollFrame.origin.x = 0;

    self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    NSLog(@"TableView Frame %@", NSStringFromCGRect(scrollFrame));
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];

}

NSLog输出为:{{0,180},{375,773}}

接下来代码运行并且每个测试结束(在此示例2中)将tableView添加到新添加的scrollView。像这样......

-(void)addSpeedTable {


    CGRect frame = self.tableView.frame;
    frame.size.height = (40 * resultHeadings.count)+35;
    frame.size.width = self.view.frame.size.width;

    //If more than one table has been shown increase Y axis height for view
    if(tablesDisplayed > 0) {
        NSLog(@"Tables displayed greater than zero");
        viewHeight = viewHeight + frame.size.height;
    }

    frame.origin.y = viewHeight;
    frame.origin.x = 0;



    self.tableView = [[UITableView alloc] initWithFrame:frame];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 40;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 22;
    self.tableView.scrollEnabled = NO; // Disable ScrollView


    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView = _tableView;
    NSLog(@"TableView Frame %@", NSStringFromCGRect(frame));

    [self.scrollView addSubview:self.tableView];
    tablesDisplayed++;
}

因为我需要将scrollView的内容大小增加,因为TableViews被添加到ScrollView我创建了一个方法,每次添加一个表时都会更新scrollView内容大小:

现在代码如下:

-(void)addScrollView {

    //Add Scroll View
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
    CGRect scrollFrame;
    scrollFrame.size.height = self.view.frame.size.height - navHeight - 150; //set height minus nav bar and top section
    scrollFrame.size.width = self.view.frame.size.width;
    scrollFrame.origin.y = 180;
    scrollFrame.origin.x = 0;

    self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    self.scrollView.delegate = self;

    [self.view addSubview:self.scrollView];

}

#pragma mark Update Scroll View Content Size
-(void)updateScrollSize {

//View height is set in the add table method depending on the size of the table
    CGFloat scrollHeight = 400 + viewHeight;

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, scrollHeight);

}

-(void)addSpeedTable {


    CGRect frame = self.tableView.frame;
    frame.size.height = (40 * resultHeadings.count)+50;
    frame.size.width = self.view.frame.size.width;

    //If more than one table has been shown increase Y axis height for view
    if(tablesDisplayed > 0) {
        NSLog(@"Tables displayed greater than zero");
        viewHeight = viewHeight + frame.size.height;
    }

    frame.origin.y = viewHeight;
    frame.origin.x = 0;
    [self updateScrollSize];    //Update scrollView contentSize when adding new table to view


    self.tableView = [[UITableView alloc] initWithFrame:frame];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 40;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 22;
    self.tableView.scrollEnabled = NO; // Disable ScrollView


    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView = _tableView;
    NSLog(@"TableView Frame %@", NSStringFromCGRect(frame));

    [self.scrollView addSubview:self.tableView];
    tablesDisplayed++;
}

1 个答案:

答案 0 :(得分:2)

您没有设置scrollView.contentSize

相关问题