Search code examples
iosswiftwebkitcollectionview

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


I am making an app with a UICollectionView that shows different websites from an array ( i am sure there are no duplicate links!! )

my problem is with duplicate UICollectionViewCells showing the same website when I scroll fast ...

I have read all the answers on how to solve the same problem for duplicate "images" in a UICollectionView but I couldn't do the same for my websites

this is my website array and the 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")



}

this is my extension for the collectionView code:

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 )

}

and this is my UICollectionViewCell code:

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)


    }

}

Solution

  • When you say

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

    What this means is that you are reusing the same cell that was used for some precious element. Hence they are being duplicated.

    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
    }
    } 
    

    We are now hiding the view till new URL is loaded, hope this solves the problem.