gpt4 book ai didi

ios - 将 UIWebView 转换为具有动态大小的 UITableViewCell

转载 作者:可可西里 更新时间:2023-11-01 05:20:41 24 4
gpt4 key购买 nike

我将 UIWebView 对象分成多个 UITableViewCell。每个 webView 都有不同的大小。最初,所有单元格的高度都相同。一旦用户触摸单元格,它就会展开以显示所有 webView 内容。

我正在使用 - (void) webViewDidFinishLoad:(UIWebView *)aWebView 动态获取 webView 高度,以及 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath 调整单元格的大小。但是,webViewDidFinishLoad 仅在调整大小方法之后调用,因此,调整大小是使用 webView 高度的旧值进行的。

我尝试按所需顺序手动调用这两种方法,但它确实有效(我认为我根本不应该这样做,无论如何......)。

这是我将 WebView 加载到每个单元格中的代码:

    [self.webView setUserInteractionEnabled:NO];
[self.webView loadHTMLString:finalString baseURL:nil];
[cell addSubview:self.webView];

cell.accessoryView = accessoryLabel;
cell.textLabel.text = nil;

self.selectedRowIndex = indexPath.row;

[tableView beginUpdates];
[tableView endUpdates];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

动态获取单元格大小:

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

CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;

self.cellSize = fittingSize.height;

NSLog(@"Calling webViewDidFinishLoad. Cell size value: %lf", self.cellSize);

}

更改单元格大小:

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

if(indexPath.row == self.selectedRowIndex) {
NSLog(@"Resizing cell with size: %lf", self.cellSize);
return self.cellSize + 10;
}

return 44;
}

输出结果如下:

2015-02-24 22:59:08.902 testProject[62200:2703641] Resizing cell with size: 0.000000
2015-02-24 22:59:09.064 testProject[62200:2703641] Calling webViewDidFinishLoad. Cell size value: 501.000000

有没有办法只在 webViewDidFinishLoad 执行后调整单元格的大小?

非常感谢任何帮助!!

最佳答案

我仍然不知道为什么会这样,但我使用以下方法解决了它:

我将 [tableView beginUpdates][tableView endUpdates] 指令移动到 webViewDidFinishLoad 方法。这样,每当执行 webViewDidFinishLoad 时,它都会正确更新单元格大小。虽然我认为这不是最好的解决方案,但它确实有效。

现在,我的 webViewDidFinishLoad 方法如下所示:

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

CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;

self.cellSize = fittingSize.height;

[self.tableView beginUpdates];
[self.tableView endUpdates];

NSLog(@"Calling webViewDidFinishLoad. Cell size value: %lf", self.cellSize);

}

也许答案可以帮助遇到类似问题的其他人。

关于ios - 将 UIWebView 转换为具有动态大小的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com