iOS ScrollView无法滚动初始加载

时间:2014-09-08 15:01:25

标签: ios uiscrollview

在viewWillAppear上发出URL请求,数据存储在SQLite中,然后调用该函数生成列表。在初始加载时调用相同的函数,如列表上方的按钮/选项卡,但初始加载不会滚动,而单击按钮,调用generateList,使滚动工作。

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *url = [NSString stringWithFormat: @"%@%@", @"www.URL.com/", @"extension";
    NSLog(@"Log: %@", url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(connection){
        //connected
    }else{
        //failed
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    //response defined in synthesize
    response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSString *extension = [NSString stringWithFormat:@"%@", connection.currentRequest];
    if(extension == @"list"){
        ...load into sqlite after deleting old data...
        [self generateList:0]
    }
}
- (IBAction)TabOne:(id)sender {
    ...style buttons...
    [self generateList:1];
}

-(void)generateList:(int)tabNum{
    // --- Problem Area ---
    // To Top of List
    [self.ListScroll setContentOffset:CGPointZero animated:NO];
    // Remove any List subviews
    [[self.List subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ...query sqlite and load content into UIView inside UIScrollView which the NSLog shows is working...
    // Resize List
    self.List.frame = CGRectMake(self.List.frame.origin.x, self.List.frame.origin.y, self.List.frame.size.width, (rowCount) *51);
    self.ListScroll.contentSize = CGSizeMake(self.List.frame.size.width, self.List.frame.size.height);
    NSLog(@"height: %f", self.List.frame.size.height);// Returns Correct
    NSLog(@"height: %f", self.ListScroll.contentSize.height);// Returns Correct
}

旁注:在故事板中,我将UIScrollView高度中的UIView设置为9,999,因为与超出这些初始界限的按钮进行交互无法正常工作。

我非常感谢解决这个问题并改进此代码的任何帮助和批评。你真棒!

1 个答案:

答案 0 :(得分:0)

我能找到的唯一解决方案是以编程方式创建UIScrollView和内部UIView,而不是使用Storyboard。如果有人有使用故事板的解决方案,我相信其他人会很感激。