根据iOS中的UIWebView内容计算表格单元格高度?

Posted

技术标签:

【中文标题】根据iOS中的UIWebView内容计算表格单元格高度?【英文标题】:Calculate table cell height according to the UIWebView content in iOS? 【发布时间】:2014-08-18 13:35:11 【问题描述】:

我正在创建基于表格视图的应用程序,当用户点击任何表格行时,我设置了一个 UIWebView 以显示为具有不同 html 内容的可扩展 UITableView。它工作正常,但我的问题是如何根据 html 内容设置单元格高度,*** 中有很多类似的问题,但我的情况有些不同。我知道如何使用 UIWebView 委托方法计算内容高度,但我如何在这里申请?

这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return [self.arrayHtmlDescription count];


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

    UIWebView *webView;
    static NSString* cellIdentifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        webView = [[UIWebView alloc] init];
        webView.frame = CGRectMake(0, 0, cell.bounds.size.width, 500);
    

    webView.userInteractionEnabled = NO;
    [webView sizeToFit];
    webView.delegate = self;
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [webView loadHTMLString: [[self.arrayHtmlDescription objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] baseURL: nil];

    [cell.contentView addSubview:webView];

    return cell;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return 1;


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

    return 500; // need to set this height dynamically

UIWebView高度计算代码:

- (void)webViewDidFinishLoad:(UIWebView *)webView 
    webView.scrollView.scrollEnabled = NO;
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    NSLog(@"webview height is : %f", fittingSize.height);

请建议我如何将 FittingSize 传递给 heightForRowAtIndexPath 表视图方法。谢谢!

【问题讨论】:

计算完webview高度后,重新加载cell,这样heightForRowAtIndexPath:委托方法就会被调用。在这里使用高度。 好的谢谢@jithinroy,我已经更新了我的代码,请检查。 你能告诉我我需要在哪里重新加载表格单元@jithinroy?谢谢! 我的意思是,在 webViewDidFinishLoad 的末尾:调用以下内容: [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];您需要为 webview 计算单元格的 NSIndexPath。 但我不知道如何计算 webview 的单元格的 NSIndexPath?我已将 1 定义为 numberOfRowsInSection。 【参考方案1】:

为表格中的每个单元格保留一个高度为 NSMutableArray 的值,并在委托方法中使用该值:

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

    return [self.height array objectAtIndex:indexPath.row]; 

当你计算 webview 的高度时,更新这个数组并调用 reloadRowsAtIndexPaths 方法或重新加载整个 tableview。

- (void)webViewDidFinishLoad:(UIWebView *)webView 
    // Caluclate the height and update the array of heights.
[self.tableview reloadData];



编辑: 要计算 NSIndexPath,您可以使用 UIWebView 的 tag 属性,如下所示:

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

    // Your code to create webview

     webview.tag = indexPath.row;
    [cell.contentView addSubview:webView];

    return cell;

现在计算高度后,您可以使用以下方法创建索引路径:

- (void)webViewDidFinishLoad:(UIWebView *)webView 
        // Caluclate the height and update the array of heights.
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:webView.tag inSection:0];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]] withRowAnimation:UITableViewRowAnimationFade];



    

【讨论】:

实际上我只为 numberOfRowsInSection 中的每个部分定义了 1 行。所以,webview.tag 每次都是 0,第二个问题是我是否会使用 [self.tableview reloadData];在 webViewDidFinishLoad 方法中,然后这个方法将无限次调用,因为我已经在我的 CellForRowAtIndexPath 中创建了 UIWebView。 使用 webView.tag = indexPath.section 并且在 webViewDidFinishLoad indexPath 将是: NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:webView.tag];而不是 reloadData 尝试 reloadRowsAtIndexPath

以上是关于根据iOS中的UIWebView内容计算表格单元格高度?的主要内容,如果未能解决你的问题,请参考以下文章

调整表格视图单元格以显示 UITextView 和 UIWebView

UIWebView 取内容高度 iOS

如何根据集合视图中的单元格选择来控制表格视图的内容?

带有 UIWebview 的 UITableViewCell,可根据内容大小调整高度

根据表格视图单元格中的内容更改 uilabel 高度

根据子WebView iOS增加单元格高度