在具有不同高度的表视图单元格中嵌入长UIWebView

时间:2021-12-24 08:16:20

I want to be able to embed a UIWebView into a tableview's cell (grouped style).

我希望能够将UIWebView嵌入到tableview的单元格中(分组样式)。

The web view is long (longer than the screen), and I want it to display it's full length. So there is no scrolling within the web view itself, just on the table.

Web视图很长(比屏幕长),我希望它显示它的全长。因此,Web视图本身没有滚动,就在桌面上。

------------------------
| a normal table cell  |
------------------------
| a normal table cell 2|
------------------------
|   a long webview     |
|  which doesn't scroll|
|  within itself       |
|                      |
|                      |
|                      |
|                      |
|                      |
|                      |
|                      |
|                      |
|                      |
|                      |
|                      |
------------------------

The web view will have various heights so how can I discover the the height of the webview in order to adjust the height in heightForRowAtIndexPath?

Web视图将具有不同的高度,因此我如何才能发现webview的高度以调整heightForRowAtIndexPath的高度?

1 个解决方案

#1


0  

Here's how you could do it, but see my caution below:

这是你如何做到的,但请看下面的注意事项:

You could load the web view, then use a javascript function to determine the height of the content. You could then use [myWebView stringByEvaluatingJavaScriptFromString: ..] to get the height.

您可以加载Web视图,然后使用javascript函数来确定内容的高度。然后,您可以使用[myWebView stringByEvaluatingJavaScriptFromString:..]来获取高度。

Here's the problem. UIWebViews are fairly slow. The table can't present itself until it knows the heights of the rows, because that's how it determines which table cells to fetch. So scrolling your table view will be jerky, because every time you scroll down to a new cell, the height will need to be computed.

这是问题所在。 UIWebViews相当慢。在知道行的高度之前,表不能呈现自己,因为这就是它如何确定要获取的表单元格。因此,滚动表格视图会不稳定,因为每次向下滚动到新单元格时,都需要计算高度。

There are two approaches you can take:

您可以采取两种方法:

1) Don't use a UIWebView embedded in a UITableCell. Instead use a UILabel and determine its height using some of the NSString convenience methods for doing this.

1)不要使用UITableCell中嵌入的UIWebView。而是使用UILabel并使用一些NSString方便方法来确定其高度。

2) Use a UIWebView for the entire table. You can very closely simulate a tableview by doing this, and you will get the UI you need. To handle things like clicks, use the URL loading hooks provided by UIWebViewDelegate.

2)对整个表使用UIWebView。您可以通过这样做非常接近地模拟tableview,您将获得所需的UI。要处理点击之类的事情,请使用UIWebViewDelegate提供的URL加载挂钩。

#1


0  

Here's how you could do it, but see my caution below:

这是你如何做到的,但请看下面的注意事项:

You could load the web view, then use a javascript function to determine the height of the content. You could then use [myWebView stringByEvaluatingJavaScriptFromString: ..] to get the height.

您可以加载Web视图,然后使用javascript函数来确定内容的高度。然后,您可以使用[myWebView stringByEvaluatingJavaScriptFromString:..]来获取高度。

Here's the problem. UIWebViews are fairly slow. The table can't present itself until it knows the heights of the rows, because that's how it determines which table cells to fetch. So scrolling your table view will be jerky, because every time you scroll down to a new cell, the height will need to be computed.

这是问题所在。 UIWebViews相当慢。在知道行的高度之前,表不能呈现自己,因为这就是它如何确定要获取的表单元格。因此,滚动表格视图会不稳定,因为每次向下滚动到新单元格时,都需要计算高度。

There are two approaches you can take:

您可以采取两种方法:

1) Don't use a UIWebView embedded in a UITableCell. Instead use a UILabel and determine its height using some of the NSString convenience methods for doing this.

1)不要使用UITableCell中嵌入的UIWebView。而是使用UILabel并使用一些NSString方便方法来确定其高度。

2) Use a UIWebView for the entire table. You can very closely simulate a tableview by doing this, and you will get the UI you need. To handle things like clicks, use the URL loading hooks provided by UIWebViewDelegate.

2)对整个表使用UIWebView。您可以通过这样做非常接近地模拟tableview,您将获得所需的UI。要处理点击之类的事情,请使用UIWebViewDelegate提供的URL加载挂钩。