2016-12-15 159 views
0

我有一個項目是你登錄後,圖像從網上加載到一個collectionView。我將部分數設置爲1,並且第一次登錄時,所有內容都正確顯示。 我可以看到下面的順序:UICollectionView cellForItemAt第二次沒有調用

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

在reloadDat AI得到以下情形:

  • numberOfSection(這裏給1)
  • numberOfItemsInSection 4
  • sizeForItemAt
  • cellForItemAt被調用,並設置我的圖片,我想滾動
  • 之間

雖然第二次我退出和收集視圖加載後,我得到相同的情況,雖然後reloadData圖像下載時沒有事情正在發生,沒有numberOfSection什麼也沒有。 我真的嘗試了許多不同的解決方案,但沒有任何工作。 由於cellForItemAt和sizeForItemAt不叫我沒有得到任何圖像顯示在我第二次登錄

我試圖numberOfItemsInSection靜態設置爲2,靜態提供兩個圖像,並且正在從那時起sizeForItemAt被稱爲等

我的數據源和委託在故事板設置爲正確的控制器

我真的卡在這個問題,讓所有的幫助表示讚賞

回答

0

問題解決了,這是我downloadTask緩存的我的圖片第二輪和重裝數據未在downloadtask回調中調用

相關問題