UICollectionView cellForItemAt未第二次调用

时间:2016-12-15 16:01:53

标签: swift uicollectionview

我有一个项目,你登录后,图像从网络加载到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等工作

我的dataSource和delegate在故事板中设置为正确的Controller

我真的遇到了这个问题所以非常感谢所有的帮助

1 个答案:

答案 0 :(得分:0)

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

相关问题