【问题标题】:How to determine UIWebView height based on content inside a variable height UITableView?如何根据可变高度 UITableView 内的内容确定 UIWebView 高度?
【发布时间】:2017-02-16 11:53:54
【问题描述】:

我有一个 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;
    }

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

【问题讨论】:

  • 使用“heightForRowAtIndexPath”中的“boundingRect”动态查找字符串高度,然后返回该高度。希望它会工作
  • 使用本教程来做到这一点samrayner.com/posts/dynamic-tableview-cells。我不喜欢在每个单元格中重复 UIWebView 的想法。 UIWebView 消耗大量内存,forums.developer.apple.com/thread/25526
  • - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { UITableViewCell *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath];返回 cell.bounds.size.height; }

标签: ios objective-c uitableview uiwebview


【解决方案1】:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { 
      UITableViewCell   *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath]; 
      return cell.bounds.size.height; 
}

【讨论】:

    猜你喜欢
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2012-10-06
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多