2011-05-24 30 views
0

我想将UIWebView添加到单元格中。 HTML数据将会改变,我会在发生这种情况时调用reloadData。将UIWebView合并到UITableViewCell中

问题是,UIWebView很好地改变,但我无法让UITableViewCell正确匹配高度。我试过,在这个方案失败...

当网页流量负载:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView { 
    CGRect frame = aWebView.frame; 
    int old_height = frame.size.height; 
    frame.size = CGSizeMake(280, 0); 
    aWebView.frame = frame; 
    float content_height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]; 
    frame = aWebView.frame; 
    frame.size = CGSizeMake(280, content_height + 20); 
    aWebView.frame = frame; 
    NSLog(@"SIZES - %i - %i",old_height + 4,(int) frame.size.height); 
    if(old_height + 4 != frame.size.height){ 
     [self.tableView reloadData]; 
    } 
} 

细胞退回高度:

return webview.frame.size.height + 20; 

第一负载单元不是尺寸不正确后。很难弄清楚如何做到这一点。我需要向下拉伸整个内容以适合单元格。

谢谢。

+0

它是tableview中的单个Web视图还是每个单元一个?表中是否有其他行? – 2011-05-24 23:42:51

+0

它有3个部分各有一行。中间部分有uiwebview。 – 2011-05-25 18:26:59

回答

-4

UIWebView不适用于另一个滚动条(如UITableView);

而要调整你应该使用UITableView.rowHeight财产或以下UITableViewDelegate方法进行的单元:

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

顺便说一下,[UITableView的reloadData]通常是用于更新表一个错误的决定。

+0

我还要如何更新表格? – 2011-05-25 18:27:31

+0

谢谢,但它不回答我的问题。你展示了我已经使用的方法,并没有告诉我如何正确地改变高度。 – 2011-05-25 18:28:17

+1

你可以调用[table beginUpdates]; [表endUpdates];强制行高计算。 – 2011-05-26 03:13:06

13

进入这个相同的问题,这是我解决它的方式。

从我可以告诉UIWebView不会正确计算它的高度,直到它添加超级视图根植于一个窗口。所以我做的是1)创建一个alpha为0的WebView,然后2)将它添加到我的UIViewController的视图中,然后3)将它重新还原到我的UITableViewCell,一旦它的加载并将alpha设置为1,这就是我的webViewDidFinishLoad看起来像。

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    CGRect frame = webView.frame; 
    frame.size = [webView sizeThatFits:CGSizeZero]; 
    frame.size.height += 20.0f; // additional padding to push it off the bottom edge 
    webView.frame = frame; 

    webView.delegate = nil; 

    UITableViewCell *cell =[_cells objectAtIndex:webView.tag]; 
    [cell.contentView addSubview:[_loadingWebView autorelease]]; 
    cell.contentView.frame = frame; 
    [cell setNeedsLayout]; 

    webView.alpha = 1.0f; 

    [self.tableView beginUpdates]; 
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:webView.tag]]; 
    [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView endUpdates]; 

    // Storing the currently loading webView in case I need to clean it up 
    // before it finishes loading. 
    _loadingWebView = nil; 
} 

在我的情况,我没有重用我的表视图细胞和我有每节Web视图一行,所以相应地调整你的代码。

+0

史诗upvote。如果可以的话,我会给你100分。 :) – 2013-01-25 06:53:14

+0

有人可以翻译它迅速? – 2015-06-25 00:09:15

+0

@卡尔文,你可以在github上发布答案,这对于其他人来说非常有用。请问? – 2016-05-18 19:36:36

0
NSString *htmlStr = Your html text; 
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(kScreenWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil]; 
CGFloat htmlHight = CGRectGetHeight(rect); 
相关问题