试图在UICollectionViewCells上同时加载不同的网站,但滚动时它们是重复的

问题描述 投票:1回答:2

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

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

我已经阅读了有关如何在UICollectionView中解决重复“图像”的相同问题的所有答案,但我不能对我的网站做同样的事情

这是我的网站数组和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)


    }

}
ios swift webkit collectionview
2个回答
1
投票

当你说

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,希望这可以解决问题。


1
投票

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

override func prepareForReuse() {
    super.prepareForReuse()
    // reset properties, remove subviews etc...
}
© www.soinside.com 2019 - 2024. All rights reserved.