当没有互联网连接时,无法计算滚动视图的高度

时间:2013-04-15 09:34:46

标签: iphone ios objective-c core-data uiscrollview

我有一个动态高度的滚动视图。高度取决于我从核心数据库中获取的图像和文本。我有以下内容。

在我的ViewDidLoad我的方法getNewsItem

以下是该方法的外观。

-(void)getNewsItem{
    NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"News"];
    NSLog(@"news ID IN METHOD %d",_newsId);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"new_id = %d",_newsId];
    [fetchRequest setPredicate:predicate];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"new_id" ascending:YES];
    fetchRequest.sortDescriptors = @[descriptor];
    NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
    newsItem = [matches objectAtIndex:0];

    lblTitle.text = newsItem.pnt_new_title;
    lblTitle.font = [UIFont fontWithName:@"ShadowsIntoLightTwo-Regular" size:16.0];
    lblTitle.textColor = [UIColor whiteColor];

    NSString *datetext = [newsItem.new_date substringWithRange:NSMakeRange(0, 11)];
    lblDate.text = datetext;
    lblDate.font = [UIFont fontWithName:@"TitilliumWeb-Regular" size:15.0];

    NSString *imgUrl = @"";
    imgUrl = [NSString stringWithFormat:@"http://mosaqua.appmax.be/%@",newsItem.new_imgurlbig];
    NSData *imgData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imgUrl]];
    UIImage *image = [UIImage imageWithData:imgData];
    self.img_Picture.image = [UIImage imageWithData:imgData];


    float ratioImage = image.size.width/image.size.height;
    float ratioPlaceholder = 320/206;
    float width;
    float heigth;
    float x;
    if(ratioImage < ratioPlaceholder){
         width = 206 * ratioImage;
        heigth = 206;
    }else{
        width = 320;
        heigth = 320/ratioImage;
    }
    if (heigth < self.view.bounds.size.width){
        x = (self.view.bounds.size.width - width) / 2;
    }else{
         x = 0;
    }
    NSLog(@"x is %f",x);
    if(heigth < 206){
        self.img_titlback.frame= CGRectMake(-4, heigth-8, self.view.bounds.size.width+5, 45);
        self.lblTitle.frame = CGRectMake(40, heigth-8, self.view.bounds.size.width, 40);
        self.btnBack.frame = CGRectMake(5, heigth, 67, 28);
        self.lblDate.frame = CGRectMake(7, heigth+30, self.view.bounds.size.width, 45);
           [self updateScrollView:newsItem.pnt_new_description andHeight:heigth+60];
    }else{
        [self updateScrollView:newsItem.pnt_new_description andHeight:300];

    }
    self.img_Picture.frame = CGRectMake(x, 0, width, heigth);

}

Webview方法

-(void)updateScrollView:(NSString *)text andHeight:(float)height{

        NSLog(@"till here in scrollview");
        UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, height,self.view.bounds.size.width-30, 20)];
        [[webView scrollView] setBounces: NO];
        webView.delegate = self;
        [scrollView addSubview:webView];
        [self setWebDescription:webView];
}

1 个答案:

答案 0 :(得分:1)

那是因为你的计算取决于从互联网上加载的图像:

NSData *imgData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imgUrl]];

您应该异步加载图像,并在没有互联网连接或图像仍在加载时提供默认大小。