Uiwebview大小未设置为webview中的内容

时间:2013-05-27 09:48:49

标签: uiwebview height

我在Sectioned UiTableViewCell中给出了像这样的webview

if(section==0)
    {
      static NSString *CellIdentifier=@"Cell";
      UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if(cell == nil)
        {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];


            webView = [[UIWebView alloc] initWithFrame:CGRectZero];
            webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
            webView.tag = 1001;
            webView.userInteractionEnabled = NO;
            webView.backgroundColor = [UIColor clearColor];
            webView.opaque = NO;


            [cell addSubview:webView];
        }


     webView = (UIWebView*)[cell viewWithTag:1001];

        webView.delegate=self;
        webView.frame=CGRectMake(0, 0, 300, 1000);
        NSLog(@"current mode: %@", [[NSRunLoop currentRunLoop] currentMode]);
        [webView loadHTMLString: [NSString stringWithFormat:[arrSecCount objectAtIndex:0], indexPath.section]baseURL:nil];
         return cell;

    }

where as the content in webview is dynamic.in 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {
    if(indexPath.section==0)
     {
        NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

     return [output floatValue]+300;

      }
        }

但是当webview中的内容增加时,它会覆盖其他不适合身高的部分。我已经尝试了许多搜索但没有用的方法。 帮助我提出建议

1 个答案:

答案 0 :(得分:0)

我认为UITableViewCell会在WebView的内容被加载之前获得它的高度......

建议在webview的委托方法中使用:

- (void)webViewDidFinishLoad:(UIWebView *)webView

你做了

[tableView reloadData];

这意味着你必须保持对webview的引用,而不是每次更新单元格内容时都不重新加载HTMLString ......