简体   繁体   中英

UIWebview inside UICollectionView, how to stop reloading webview on scrolling collection view

I have UIWebView inside UICollectionViewCell, whenever i scroll collection view my webview getting reloaded. It is taking so much time.

I kept some booleans to stop reloading webview, if that cell's webview is already loaded. But this causing cells mismatch issue (last loaded or other cell webview data is showing in cells)

I am loading webview using html string.

cell.webView.loadHTMLString(updatedString, baseURL: nil)

My UI looks like... 在此输入图像描述

You can check whether html content is available in the webview or not. In cellForRow method if the html string is empty call loadHTMLString method.

if let htmlString = self.webView1.stringByEvaluatingJavaScript(from: "document.body.innerHTML") {
    if htmlString.isEmpty {
        self.webView1.loadHTMLString("<p>asda</p><b>asdg</b>", baseURL: nil)
    } else {
        print("html already loaded")
    }
}

Use WKWebView . UIWebView is Deprecated

let webView = WKWebView()

self.webView.evaluateJavaScript("document.body.innerHTML", completionHandler: { (string, error) in
    if let htmlString = string as? String{
        if htmlString.isEmpty {
            self.webView.loadHTMLString("<p>asda</p><b>asdg</b>", baseURL: nil)
        } else {
            print("html already loaded")
        }
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM