gpt4 book ai didi

swift - UICollectionView cellForItemAt 未第二次调用

转载 作者:行者123 更新时间:2023-11-30 12:49:34 25 4
gpt4 key购买 nike

我有一个项目,您登录后,图像从网络加载到 Collection View 中。我将节数设置为 1,第一次登录时所有内容都会正确显示。我可以看到以下顺序:

  • numberOfSection(此处给出 1)
  • 节中的项目数 0
  • insetForSectionAt

然后,当我下载图像时,我会发生一个事件,我调用以下代码 didFinishDownloadingTo

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
let url=(downloadTask.currentRequest?.url)?.pathComponents


let urlId = (url!.last)!
let offerIndex = self.downloadIndex[urlId]
let imageData=try? Data(contentsOf: location)

if imageData != nil && offerIndex != nil && self.offers[offerIndex!] != nil /* && offerIndex! < self.offers.count */ {
self.stopAutoScrolling()

//Sending to background thread since we see in profile that this takes time and hanging main thread
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {
self.downloadCache[urlId] = imageData
self.offers[offerIndex!]!.image = UIImage(data: imageData!)
self.updateCache()

DispatchQueue.main.async {
self.setupDataForCollectionView()
self.collectionView?.reloadData()
self.startAutoScrolling()
}

})

}

}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

return self.view.frame.size
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: -64, left: 0, bottom: 0, right: 0)
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("section num \(section)")

return carousel.count
}

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

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! EventOfferCell

//LoggingManager.sharedInstance.logInfo("Carousel count %i indexPath %i", carousel.count,indexPath.row)
if let offer = offerForIndexPath(indexPath) {
if let offerImage = offer.image {
cell.imageView.image = offerImage
cell.activityIndicator.stopAnimating()
cell.titleLabel.isHidden = true
}
else {
cell.titleLabel.text = offer.title
}
}

return cell
}

func setupDataForCollectionView() {
carousel.removeAll()
for (_, offer) in self.offers {
carousel.append(offer)
}
if carousel.count > 1 {
let firstOffer = carousel.first
let lastOffer = carousel.last

carousel.append(firstOffer!)
carousel.insert(lastOffer!, at: 0)
}
}

重新加载数据后,我得到以下场景:

  • numberOfSection(此处给出 1)
  • 第 4 节中的元素数量
  • sizeForItemAt
  • cellForItemAt 被调用,我设置了要在其之间滚动的图像

尽管在我注销并加载 Collection View 后第二次,我得到了相同的场景,但在下载图像时 reloadData 之后什么也没有发生,没有 numberOfSection 调用什么也没有。我确实尝试了很多不同的解决方案,但没有任何效果。由于未调用 cellForItemAt 和 sizeForItemAt,我第二次登录时没有显示任何图像

我尝试将 numberOfItemsInSection 静态设置为 2 并静态提供两个图像,从那时起 sizeForItemAt 被调用等就开始工作了

我的数据源和委托(delegate)在 Storyboard中设置为正确的 Controller

我真的被这个问题困扰了,所以感谢所有帮助

最佳答案

问题已解决,是我的 downloadTask 第二轮缓存了我的图像,并且在 downloadtask 回调中未调用 reloadData

关于swift - UICollectionView cellForItemAt 未第二次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41168388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com