将UIWebView转换为具有动态大小的UITableViewCell [英] UIWebView into a UITableViewCell with dynamic size
问题描述
我将 UIWebView
对象分成多个 UITableViewCell
。每个webView都有不同的大小。最初,所有细胞都是相同的高度。用户触摸单元格后,会展开以显示所有webView内容。
I have UIWebView
objects into multiples UITableViewCell
. Each webView has a different size. Originally, all cells are the same height. Once the user touch the cell, it expand to show all webView Content.
我正在使用 - (void)webViewDidFinishLoad:(UIWebView *)aWebView
来动态获取webView高度,和 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
来调整单元格的大小。但是, webViewDidFinishLoad
仅在调整大小方法后调用,因此,使用旧值为webView高度调整大小。
I'm using - (void) webViewDidFinishLoad:(UIWebView *)aWebView
to dynamically get the webView height, and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
to resize the cell. However, webViewDidFinishLoad
is only called after the resize method, therefore, the resize is made using old values for webView height.
我试图按照所需的顺序手动调用这两种方法,但确实有效(我不认为我应该这样做,无论如何......) 。
I tried to call both methods manually in the desired order, but it did worked (I don't think that I should do this at all, anyway...).
这是我将网页视图加载到每个单元格的代码:
Here is my code for load the web view into each cell:
[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
已被执行?
Is there a way to only resize the cell after webViewDidFinishLoad
has been executed?
我非常感谢任何帮助!!
I really appreciate any help!!
推荐答案
我还是不喜欢不知道为什么会这样,但我用以下方法解决了这个问题:
I still don't know why this is happing but I solved it using the following approach:
我搬了 [tableView beginUpdates]
和 [tableView endUpdates]
指示 webViewDidFinishLoad
方法。这样,只要执行 webViewDidFinishLoad
,它就会正确更新单元格大小。虽然我不认为这是最好的解决方案,但它确实有效。
I moved [tableView beginUpdates]
and [tableView endUpdates]
instructions to webViewDidFinishLoad
method. At this way, whenever webViewDidFinishLoad
be executed, it will updated the cell size correctly. Although I don't think that is the best solution, it works.
现在,我的 webViewDidFinishLoad
方法如下所示:
Now, my webViewDidFinishLoad
method looks like this:
- (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);
}
也许答案可以帮助其他有类似问题的人。
Perhaps the answer helps someone else with similar problems.
这篇关于将UIWebView转换为具有动态大小的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!