【问题标题】:How to stop `UIWebView` inside a `UITableViewCell` from reloading when dequeuing?出队时如何阻止`UITableViewCell`中的`UIWebView`重新加载?
【发布时间】:2017-10-19 18:57:21
【问题描述】:

Video of web view reloading after shown on screen

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 视图内容。如何在 web 视图中只加载一次页面,而不是每次单元格离开屏幕时加载它?任何帮助将不胜感激和投票。谢谢。

【问题讨论】:

    标签: ios swift uitableview uiwebview


    【解决方案1】:

    您的代码显示您根本没有使用dequeueReusableCell 的缓存功能。 您正在从缓存(队列)中获取单元格,然后向其添加新的 Web 视图。

    解决方案是在WebviewTableViewCellinit/storyboard 中创建一个网络视图。

    【讨论】:

    • 我在情节提要的 tableView 中创建了一个专用的单元格类及其对应项。在cellForRow 中,我正在使用_request.cachePolicy = .returnCacheDataElseLoad,但页面仍然会重新加载。你能帮帮我吗?
    【解决方案2】:

    试试看

    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
    

    }

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2012-05-20
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 2019-07-13
      相关资源
      最近更新 更多