2016-12-22 9 views
1

私がやっていることは、いくつかのHTMLリソースファイルをUITableViewセルに読み込むことです。私はセルの高さをUIWebViewのコンテンツサイズに応じて調整したいと思っていました。以下は私のテストコードの一部です。Dynamic UITableViewCell iOSでUIWebViewを含むセルの高さ

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"TableCellID"; 

    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; 
     cell = [nibArray objectAtIndex:0]; 
    } 

    NSString *htmlFileName = [NSString stringWithFormat:@"page%ld", (long)indexPath.row]; 

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:@"html"]; 
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    [cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]]; 

    return cell; 
} 

私は私のCustomTableViewCell.xibを記述した場合、それはコンテンツビュー内部のUIWebViewを持っているとのUIWebViewは、与えられた制約を以下ました。

ContentView.Bottom = WebView.Bottom + 8 
ContentView.Top = WebView.Top +8 
ContentView.Leading = WebView.Leading + 8 
ContentView.Trailing = WebView.Trailing + 8 

HTMLファイルにはJavaScriptまたはCSSコンテンツはありません。以下のようにシンプル。

<html> 
    <header> 
    </header> 
    <body> 
     <p>eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee</p> 
    </body> 
</html> 

私の出力は以下のようなイメージです。

enter image description here

私が一番上に説明したように、私のWebページには、任意のJavaScriptを含んでいません。だから私はstringByEvaluatingJavaScriptFromStringメソッドを使用してテキストを返すことができないかもしれません。

+0

[既に読み込まれている表のセルの高さを変更するにはどうすればよいですか?](http://stackoverflow.com/questions/20666405/how-do-i-change-the-height-of-すでにロードされているテーブルセル) –

+0

ロード後のWebviewの高さの計算が簡単になり、webViewの高さに基づいてセルを調整させることができると思います。 – Joshua

答えて

0

私は自分の解決策を思いついた。しかし、この質問にコメントして、別の道で私の心を開いてくれた人たちに感謝します。ここで私がしたこと。

最初ににフィットするサイズを計算し、HTMLページをロードし、Webビューがロードされた後、デリゲートは、以下の方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"TableCellID"; 

    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 

     NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; 
     cell = [nibArray objectAtIndex:0]; 
    } 

    cell.webView.delegate = self; 
    cell.webView.tag = indexPath.row; 
    cell.webView.scrollView.scrollEnabled = NO; 

    NSString *htmlFileName = [NSString stringWithFormat:@"page%ld", (long)indexPath.row]; 

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:@"html"]; 
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    [cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]]; 

    return cell; 
} 

内で行われる追加その後、私のViewControllerクラスに

@interface ViewController() <UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate> { 

をUIWebViewDelegateを追加しましたそれは内容で、すべてのWebビューが読み込まれた後、私は以下のようにテーブルのデータをリロードします。

- (void)webViewDidFinishLoad:(UIWebView *)webView { 

    if (webView.tag == 0) { 
     [cellHeightMutArray removeAllObjects]; 
    } 

    CGSize fittingSize = [webView sizeThatFits:CGSizeZero]; 

    [cellHeightMutArray insertObject:@(fittingSize.height) atIndex:webView.tag]; 

    if (((webView.tag + 1) == dataArray.count) && !reloadedTableViewAfterLoadingWebContent) { 

     reloadedTableViewAfterLoadingWebContent = YES; 
     _tableView.hidden = NO; 

     [_tableView reloadData]; 
    } 
} 

それから私は、これは私のソリューションであり、それはiPhoneやiPadに完璧に動作します(私が持っていた主な問題でした)セルの高さ

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

    if (cellHeightMutArray.count > 0) { 

     return [[cellHeightMutArray objectAtIndex:indexPath.row] intValue]; 
    } 

    return 44; 
} 

を調整します。

実際のプロジェクトにあるより複雑なユーザーインターフェイスをiPadsで実行すると、別の問題が発生しました。これらの問題に対する私のソリューションがこの質問に関連するなら、私はこれを更新します。

2

この

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    arrOfHTMLList= [[NSMutableArray alloc] init]; 
    NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] init]; 
    [dic1 setObject:@"0" forKey:@"height"]; 
    [dic1 setObject:@"aaaaaaaaaa aaaaaa aaaaaaa aaaaaaa aaaaaa aaaaaa aaaaaaaa aaaaa" forKey:@"htmlString"]; 

    NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init]; 
    [dic1 setObject:@"0" forKey:@"height"]; 
    [dic1 setObject:@"abababababbabababababab ababab bababa ababababb" forKey:@"htmlString"]; 

    NSMutableDictionary *dic3 = [[NSMutableDictionary alloc] init]; 
    [dic1 setObject:@"0" forKey:@"height"]; 
    [dic1 setObject:@"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:@"htmlString"]; 

    NSMutableDictionary *dic4 = [[NSMutableDictionary alloc] init]; 
    [dic1 setObject:@"0" forKey:@"height"]; 
    [dic1 setObject:@"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:@"htmlString"]; 

    [arrOfHTMLList addObject:dic1]; 
    [arrOfHTMLList addObject:dic2]; 
    [arrOfHTMLList addObject:dic3]; 
    [arrOfHTMLList addObject:dic4]; 
    } 


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    UIWebView *webView = (UIWebView*)[cell viewWithTag:1]; 
    NSString *myHTML = arrOfHTMLList[indexPath.section][@"htmlString"]; 
    [webView loadHTMLString:myHTML baseURL:nil]; 
    webView.delegate = self; 
    webView.frame = CGRectMake(0, 0, screenWidth,cell.frame.size.height); 
return cell; 
} 


-(void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    for (UIView *parent=[webView superview];parent!=nil ;parent=[parent superview]) 
    { 
     if([parent isKindOfClass:[UITableViewCell class]]) 
     { 
      UITableViewCell *cell=(UITableViewCell *)parent; 
      NSIndexPath *indexPath=[_tableView indexPathForCell:cell]; 
      int height = webView.scrollView.contentSize.height; 
      [[arrOfHTMLList objectAtIndex:indexPath.section] setObject:[NSString stringWithFormat:@"%d",height] forKey:@"height"]; 
      [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
     } 
    } 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGFloat height = [arrOfHTMLList[indexPath.section][@"height"] intValue]; 
    return height; 
} 

はそれがお役に立てば幸いです。試してみてください

+0

これは、Webビューのフレームがhtmlコンテンツにすぐに適応しないために機能しません:) - 非同期 –

+0

また、sizeToFit –

+0

@ Daij-Djanを手動で呼び出すのを忘れてしまいました。フレームの高さの代わりに "webView.scrollView.contentSize.height" –

0

主な問題は、すぐにそのHTMLコンテンツに対応するためにWebビューのフレームを取得することです:) とそれは非同期的にコンテンツをロードした後にのみ発生します!

https://github.com/Daij-Djan/DDUtils/tree/master/objC/DDUtils-IOS/ui/M42WebviewTableViewCell%20%5Bios%5D

コードの最も重要な部分は、次のとおりです:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView { 
    //TODO: update the table's height for the cell for the webview 
} 

は、私が書いたドロップインソリューションは、テーブルをリロードするのWebViewを持っており、デリゲートを通知するカスタムUITableViewCellのサブクラスであります
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    M42WebviewTableViewCell *cell = _loadedCells[@(indexPath.row)]; 
    if (!cell) { 
     cell = [[M42WebviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 

     NSString *object = _objects[indexPath.row]; 
     cell.tag = indexPath.row; 
     cell.delegate = self; 
     [cell setHtml:object AndBaseURL:nil]; 
    } 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //if a cell isnt ready, it wont be there and the method will return 0 
    M42WebviewTableViewCell *cell = _loadedCells[@(indexPath.row)]; 
    return cell.height; 
} 

#pragma mark - M42WebviewTableViewCellDelegate 

- (void)tableCellDidLoadContent:(M42WebviewTableViewCell *)cell { 
    if (!_loadedCells) { 
     _loadedCells = [[NSMutableDictionary alloc] init]; 
    } 
    [_loadedCells setObject:cell forKey:@(cell.tag)]; 

    //only refresh sizes of rows 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
} 
関連する問題