【问题标题】:UICollectionView cellForItemAt not called second timeUICollectionView cellForItemAt 未第二次调用
【发布时间】:2016-12-15 16:01:52
【问题描述】:

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

  • numberOfSection(此处为 1)
  • numberOfItemsInSection 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)
    }
}

在 reloadData 之后,我得到以下场景:

  • numberOfSection(此处为 1)
  • numberOfItemsInSection 4
  • sizeForItemAt
  • cellForItemAt 被调用,我设置了我想要在它们之间滚动的图像

虽然 第二次 在我注销并加载集合视图之后,我得到了相同的情况,尽管在 reloadData 之后下载图像时没有发生任何事情,没有 numberOfSection 没有调用任何内容。 我真的尝试了许多不同的解决方案,但没有任何效果。 由于未调用 cellForItemAt 和 sizeForItemAt 我第二次登录时没有显示任何图像

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

我的数据源和委托在情节提要中设置为正确的控制器

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

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2012-12-03
      • 1970-01-01
      • 2020-09-08
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多