2011-05-24 64 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);