如何从出队时重装停止`UIWebView`一个`UITableViewCell`里面?
问题描述:
Video of web view reloading after shown on screen如何从出队时重装停止`UIWebView`一个`UITableViewCell`里面?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if indexPath.row == 17 {
let _cell = tableView.dequeueReusableCell(withIdentifier: "webCell") as! WebviewTableViewCell
let webView = UIWebView()
webView.frame = _cell.contentView.bounds
webView.scalesPageToFit = true
let webViewURL = "https://www.reddit.com/"
var _request = URLRequest(url: URL(string: webViewURL)!)
_request.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(_request)
_cell.contentView.addSubview(webView)
return _cell
}
cell.textLabel?.text = "sample cell \(indexPath.row + 1)"
return cell
}
我用上面的代码来提供Web视图的缓存副本。但是,每当具有Web视图的单元格都不在屏幕中时,表视图就会重新加载Web视图内容。如何在网页视图中只加载一次页面,而不是每次单元格离开屏幕时加载页面?任何帮助将不胜感激和增加投票。谢谢。
答
你的代码显示了你不使用dequeueReusableCell
的缓存功能在所有。 你都可以从缓存(队列)的单元格,然后添加一个新的Web视图它。
解决方案是在WebviewTableViewCell
的init
/故事版中创建网络视图。
答
试试吧
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if indexPath.row == 17 {
let _cell = tableView.dequeueReusableCell(withIdentifier: "webCell") as! WebviewTableViewCell
If _cell.contentView.viewWithTag(100) == nil
{
let webView = UIWebView()
webView.tag=100;
webView.frame = _cell.contentView.bounds
webView.scalesPageToFit = true
let webViewURL = "https://www.reddit.com/"
var _request = URLRequest(url: URL(string: webViewURL)!)
_request.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(_request)
_cell.contentView.addSubview(webView)
}
return cell;
}
cell.textLabel?.text = "sample cell \(indexPath.row + 1)"
return cell
}
我创建了一个专门的电池类及其在故事板是对应的tableView。在'cellForRow'我使用'_request.cachePolicy = .returnCacheDataElseLoad'仍然页面重新加载得到。你能帮我解决吗? –