【发布时间】:2013-11-30 16:06:09
【问题描述】:
这个问题在Removing HTML url tags in iOS 上跟进。我决定采用 Web View 方法,因为这似乎是获得正确链接的最简单方法(也就是带有实际标题的链接,而不仅仅是 url)。但我被困在如何计算网络视图的高度上。我使用带有 Web 视图的表格视图单元格作为子视图。
我目前失败的方法:
-
表格视图询问单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [CommentTableViewCell heightForCellWithComment:(self.comments)[indexPath.row]]; }这适用于文本视图,因为我可以使用
boundingRectWithSize:options:attributes:context:来计算文本高度,在顶部和底部添加一些填充,然后返回高度。使用网络视图,我无法做到这一点。我目前从网络返回的数据,其中仍然包含一些 html 标签,插入一些 html 来更改字体等,然后在 web 视图中呈现它。如果我使用boundingRectWithSize:options:attributes:context之类的方法:返回的高度显然是不一样的。 -
这可能会尝试计算高度,但这不起作用并返回 0。
commment上的body属性包含 html 数据。+ (CGFloat)heightForCellWithComment:(Comment *)comment { CommentTableViewCell *cell = [[CommentTableViewCell alloc] init]; [cell.textWebView loadHTMLString:comment.body baseURL:nil]; cell.textWebView sizeThatFits: CGSizeMake(cell.textWebView.frame.size.width, CGFLOAT_MAX)]; return cell.textWebView.frame.size.height + 48.0f; // 48 px padding } -
UITableViewDataSource 请求单元格。我从情节提要中取出一个单元格,并在其上设置
comment属性。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; Comment *comment = ...; cell.comment = comment; return cell; }
所以我想说的是,我正在寻找一种在所有表格视图开始之前计算 Web 视图尺寸的方法。提前谢谢你。
【问题讨论】:
标签: ios objective-c uitableview uiwebview