试图在 UICollectionViewCells 上同时加载不同的网站,但是当我滚动时它们被复制了

trying to load different websites at the same time on UICollectionViewCells but they are being duplicated when I scroll

我正在制作一个带有 UICollectionView 的应用程序,它显示数组中的不同网站(我确信没有重复的链接!!)

我的问题是当我快速滚动时重复的 UICollectionViewCells 显示相同的网站...

我已经阅读了有关如何解决 UICollectionView 中重复 "images" 的相同问题的所有答案 但我不能为我的网站做同样的事情

这是我的网站数组和 viewDidLoad:

let caocapWEB = ["https://qasr-alaros.com", "https://missfeionkah.com", "https://zid.store/dal-accessories", "https://gumesh.com", "https://cnds.co", "https://nano-eshop.com","http://zf-lc.net", "https://carbonfibreauto.com","https://syarah.com", "https://procaresa.com","https://www.liqui-moly.com", "https://www.niceonesa.com","https://www.alkhdmah.com", "http://alsoukalsaudi.com","https://kyzstore.com", "http://www.otlobli.com","https://munwradates.store", "http://www.bakerycup.com.sa","https://tarafcoco.com", "https://uae.souq.com/ae-en/","http://bassurah.com", "https://raseel.gift","https://www.cvmaker.info", "http://osos-edu.com","http://www.careery.com", "http://samaacademic.net","http://btfconsult.com", "https://www.ghanaty.com","https://www.bilsaan.com", "https://glamourbeauty.com","http://marianinos.com", "https://arttime.sa","https://salla.sa/monwhite_shop", "http://shop.kawtherart.com","https://www.sokkat-alteeb.com", "https://mna9rr.com","https://mefateeh.com", "https://salla.sa/m_tiqani","https://fitneeds.sa", "https://salla.sa/slk.sha7n","https://grazie.me", "https://cd-k.com","https://hl2.store", "https://lantanaashop.com","https://quick.sa.com/home_en/", "https://business1409.com","http://www.saudi-dsc.com", "http://www.hotelzgroup.com","http://www.tafassell.com", "https://9200.co","https://www.mhatat.com", "http://cptltd.com","https://www.barigallery.com", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "","", "", ""]

override func viewDidLoad() {
    super.viewDidLoad()


    caocapsCollectionView.delegate = self
    caocapsCollectionView.dataSource = self

    caocapsCollectionView.register(UINib.init(nibName: "caocapCell", bundle: nil), forCellWithReuseIdentifier: "caocapCell")



}

这是我对 collectionView 代码的扩展:

extension ExploreVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout  {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return caocapWEB.count
}



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

    let caocap = caocapWEB[indexPath.row]
    guard let cell = caocapsCollectionView.dequeueReusableCell(withReuseIdentifier: "caocapCell", for: indexPath) as? caocapCell else { return UICollectionViewCell() }

         let theURL = URL(string: caocap)
        let defaultValue = URL(string: "https://ficruty.wixsite.com/caocap")!
        var urlRequest = URLRequest(url: theURL ?? defaultValue)
        urlRequest.cachePolicy = .returnCacheDataElseLoad
        cell.configureCell(website: urlRequest)

    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let numberOfColumns: CGFloat = 2
    let width = collectionView.frame.size.width
    let height = collectionView.frame.size.height
    let xInsets: CGFloat = 10
    let cellspacing: CGFloat = 5

    return CGSize(width: (width / numberOfColumns) - (xInsets + cellspacing ) , height: height / 1.8 )

}

这是我的 UICollectionViewCell 代码:

import UIKit
import WebKit

class caocapCell: UICollectionViewCell {

@IBOutlet weak var webView: WKWebView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    }

func configureCell(website: URLRequest) {

    self.webView.load(website)


    }

}

当你说

guard let cell = caocapsCollectionView.dequeueReusableCell(withReuseIdentifier: "caocapCell", for: indexPath) as? caocapCell else { return UICollectionViewCell() }

这意味着您正在重复使用用于某些珍贵元素的同一个单元格。因此它们被复制了。

class caocapCell: UICollectionViewCell, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!


func configureCell(website: URLRequest) {

    if self.webView.isLoading {
        self.webView.stopLoading()
    }

    self.webView.isHidden = true

    self.webView.navigationDelegate = self
    self.webView.load(website)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    self.webView.isHidden = false
}
} 

我们现在隐藏视图,直到加载新的 URL,希望这能解决问题。

您应该重写 prepareForReuse() 方法并重置您要在单元格中重用的所有属性:

override func prepareForReuse() {
    super.prepareForReuse()
    // reset properties, remove subviews etc...
}