2016-12-15 29 views
0

ログインした後、画像がwebからcollectionViewに読み込まれました。セクションの数を1に設定し、初めてログインすると正しく表示されます。UICollectionView cellForItemAtが2回目に呼び出されない

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

リロード後次のシナリオを取得愛:

  • (ここでは1を与える)numberOfSection
  • numberOfItemsInSection 4
  • sizeForItemAt
  • cellForItemAtが呼び出され、私は

間をスクロールしたい私の画像を設定されていますしかし、ログアウトしてコレクションビューがロードされた後にの第2回目が返されますが、イメージがダウンロードされたときにreloadDataの後に同じシナリオが表示されます何も起こっていない、numberOfSectionは何も呼び出さない。 私は本当に多くの異なるソリューションを試しましたが、何も動作しません。 cellForItemAtとsizeForItemAtが呼び出されないので は、私は静的2にnumberOfItemsInSectionを設定し、静的に2枚の画像を提供しようとしていると、それは、その後sizeForItemAtが呼び出されるので、働いている

ログインなしの画像が二度目に表示されません取得など

私のデータソースとデリゲートが正しいコントローラにストーリーボードに設定されている私は本当にこの問題で立ち往生しています

ので、すべてのヘルプは高く評価され

答えて

0

問題は、それが第二ラウンドとリロード私の画像をキャッシュされた私のdownloadTaskた解決しましたダウンロードタスクのコールバックでデータが呼び出されなかった

関連する問題