根据 webview 内容更改 UITableViewCell 高度
Change UITableViewCell height based on webview content
我有一个 UIWebView 嵌套在 UITableViewCell 中。
如何根据加载到 WebView 中的内容(即完成下载后)调整单元格的高度...?
我有表视图将 heightForRowAtPath 设置为 UITableViewAutomaticDimension
。
loadContentIntoWebview
由 TableView 的所有者在其 cellForRowAt
委托方法中调用。
我注意到 heightForRowAt
在 cellForRowAt
之前被调用,所以我 "solution" 根据单元格的 webview 的内容大小设置高度将不起作用。
import UIKit
class MyCell: UITableViewCell, UIWebViewDelegate {
// MARK: - instance variables
@IBOutlet weak var myWebview: UIWebView?
// MARK: - lifecycle methods
override func awakeFromNib() {
super.awakeFromNib()
myWebview?.scrollView.bounces = false;
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// MARK: - private methods
func loadContentIntoWebview(htmlString: String) {
myWebview?.loadHTMLString(htmlString, baseURL: nil)
}
// MARK: - UIWebViewDelegate methods
func webViewDidFinishLoad(_ webView: UIWebView) {
// dynamically set WebView height
let tempString = myWebview?.stringByEvaluatingJavaScript(from: "document.body.scrollHeight")
var tempHeight: CGFloat = 0.0
if let n = NumberFormatter().number(from: tempString!) {
tempHeight = CGFloat(n)
}
myWebview?.frame.size.height = tempHeight
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let cell :yourCell = self.yourTblVw.cellForRow(at: yourindexpath) as? yourCell{
// set height of webview as per its content
var frame = cell.webView.frame
frame.size.height = 1
let fittingSize = cell.webView.sizeThatFits(CGSize.zero)
frame.size = fittingSize
cell.heightWebVwMain.constant = fittingSize.height
}
其中 heighWebVwMain 是单元格 webview 高度的约束。所以你也必须在 cellforRow 方法中设置这个常量
找到解决方案...
- 在
webViewDidFinishLoad(_ webView: UIWebView)
中设置约束的
高度与最初的问题相同,但使用 document.body.clientHeight
- 发出通知,告诉关联的表视图不是
reloadData()
,而是 beginUpdates()
,然后是 endUpdates()
@objc func handleWebviewFinishedLoading() {
self.webview?.beginUpdates()
self.webview?.endUpdates()
}
这似乎是在不重新加载单元格的情况下重新绘制单元格:)
我有一个 UIWebView 嵌套在 UITableViewCell 中。
如何根据加载到 WebView 中的内容(即完成下载后)调整单元格的高度...?
我有表视图将 heightForRowAtPath 设置为 UITableViewAutomaticDimension
。
loadContentIntoWebview
由 TableView 的所有者在其 cellForRowAt
委托方法中调用。
我注意到 heightForRowAt
在 cellForRowAt
之前被调用,所以我 "solution" 根据单元格的 webview 的内容大小设置高度将不起作用。
import UIKit
class MyCell: UITableViewCell, UIWebViewDelegate {
// MARK: - instance variables
@IBOutlet weak var myWebview: UIWebView?
// MARK: - lifecycle methods
override func awakeFromNib() {
super.awakeFromNib()
myWebview?.scrollView.bounces = false;
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// MARK: - private methods
func loadContentIntoWebview(htmlString: String) {
myWebview?.loadHTMLString(htmlString, baseURL: nil)
}
// MARK: - UIWebViewDelegate methods
func webViewDidFinishLoad(_ webView: UIWebView) {
// dynamically set WebView height
let tempString = myWebview?.stringByEvaluatingJavaScript(from: "document.body.scrollHeight")
var tempHeight: CGFloat = 0.0
if let n = NumberFormatter().number(from: tempString!) {
tempHeight = CGFloat(n)
}
myWebview?.frame.size.height = tempHeight
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let cell :yourCell = self.yourTblVw.cellForRow(at: yourindexpath) as? yourCell{
// set height of webview as per its content
var frame = cell.webView.frame
frame.size.height = 1
let fittingSize = cell.webView.sizeThatFits(CGSize.zero)
frame.size = fittingSize
cell.heightWebVwMain.constant = fittingSize.height
}
其中 heighWebVwMain 是单元格 webview 高度的约束。所以你也必须在 cellforRow 方法中设置这个常量
找到解决方案...
- 在
webViewDidFinishLoad(_ webView: UIWebView)
中设置约束的 高度与最初的问题相同,但使用document.body.clientHeight
- 发出通知,告诉关联的表视图不是
reloadData()
,而是beginUpdates()
,然后是endUpdates()
@objc func handleWebviewFinishedLoading() {
self.webview?.beginUpdates()
self.webview?.endUpdates()
}
这似乎是在不重新加载单元格的情况下重新绘制单元格:)