如何根据可变高度 UITableView 中的内容确定 UIWebView 高度?

How to determine UIWebView height based on content inside a variable height UITableView?

我有一个 UITableView 单元格,它的大小会根据其内容(可能是几行文本)而变化。

由于 heightForRowAtIndexPath 似乎是在我对单元格进行布局之前调用的,所以我只是通过在我的文本字符串上调用 [NSString sizeWithFont] 来猜测正确的高度。在我在单元格中布置好文本并知道它应该是什么大小之后,是否有更好的方法来设置高度?

我的问题是每个单元格都包含一个 UIWebView,内容不同(静态加载)我不知道如何根据内容计算正确的高度。有没有办法做到这一点?我试过这样的事情:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       WebViewCell *cell = (WebViewCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
       [cell setNeedsLayout];
       [cell layoutIfNeeded];
       return cell.bounds.size.height;
    }

单元格本身是从笔尖加载的,笔尖只是一个包含 UIWebViewUITableViewCell。 (如果单元格只是将自己调整为最大的 html 内容也很好,尽管可变高度会更好)。

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { 
      UITableViewCell   *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath]; 
      return cell.bounds.size.height; 
}