簡體   English   中英

如何在 UICollectionViewCell 中使用 WKWebView?

[英]How to use WKWebView inside UICollectionViewCell?

我想在UICollectionview中加載HTML String。每個collectionviewcell都有WKWebview。 當我們嘗試加載更多 uicollectionviewcell 應用程序時,它會崩潰而不會顯示任何錯誤。 我進一步查看 memory 選項卡和 memory 頁面其他進程 memory 在每個 wkwebview loadhtmlstring 被觸發時都會增長。 如何在 swift4 中刪除 WKWebview memory ... 或者除了 WKWebview 之外,是否有任何控件可以顯示和編輯 iOS 中的 HTML 內容? 請幫我解決這個問題。 提前致謝..

注意:我已經嘗試過堆棧溢出WKWebView 中建議的這種方法導致我的視圖 controller 泄漏

請就此提出任何其他建議。

您可以緩存 HTML 頁面的響應,在單元格消失或不在視圖中后,您可以從視圖中刪除這些單元格,當您查看該單元格時,您可以從緩存中選擇單元格信息,這樣您就可以保存 memory.d

我得到了同樣的工作。 您可以使用下面提到的代碼。

首先你只需要“導入WebKit”框架。

class SpecificLessonDetailCVC: UICollectionViewCell {
    //MARK:- IBOUTLET & PROPERTIES
    @IBOutlet weak var containerView: UIView!
    let webView = WKWebView()
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt  indexPath: IndexPath) -> UICollectionViewCell {

guard let cell = collVwLesson.dequeueReusableCell(withReuseIdentifier: "SpecificLessonDetailCVC", for: indexPath) as? SpecificLessonDetailCVC else { return UICollectionViewCell()}
        
        cell.webView.scrollView.showsHorizontalScrollIndicator = false
        cell.webView.scrollView.showsVerticalScrollIndicator = false
        cell.webView.scrollView.indicatorStyle = .white
        cell.webView.scrollView.delegate = self
        cell.webView.navigationDelegate = self
        cell.webView.loadHTMLStringWithMagic(content: objLessonDetailsVM.responseModel?.data.data[indexPath.row].dataDescription ?? "", baseURL: nil)
        cell.webView.frame = CGRect(x: 0, y: 0, width: cell.vwLesson.frame.width, height: cell.vwLesson.frame.height)
        cell.vwLesson.addSubview(cell.webView)
        
        return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if let cell = cell as? SpecificLessonDetailCVC {
            cell.webView.becomeFirstResponder()
        }
     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM