2015-05-28 66 views
1

我有一個UITableViewUICollectionView洞察每個表視圖單元格。我使用UICollectionView視圖作爲圖庫(帶分頁的集合視圖)。我的邏輯是這樣的: 洞察方法單元格洞察UICollectionView在可見之前不會加載?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // This is a dictionary with an index (for the table view row), 
    // and an array with the url's of the images 
    self.allImagesSlideshow[indexPath.row] = allImages 

    // Calling reloadData so all the collection view cells insight 
    // this table view cell start downloading there images    
    myCell.collectionView.reloadData() 
} 

我打電話collectionView.reloadData(),並在

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    // This method is called from the cellForRowAtIndexPath of the Table 
    // view but only once for the visible cell, not for the all cells, 
    // so I cannot start downloading the images 


    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoCollectionCell 

    if self.allImagesSlideshow[collectionView.tag] != nil { 

     var arr:[String]? = self.allImagesSlideshow[collectionView.tag]! 

     if let arr = arr { 

      if indexPath.item < arr.count { 

       var imageName:String? = arr[indexPath.item] 

       if let imageName = imageName { 

        var escapedAddress:String? = imageName.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) 

        if let escapedAddress = escapedAddress { 

         var url:NSURL? = NSURL(string: escapedAddress) 

         if let url = url { 

          cell.imageOutlet.contentMode = UIViewContentMode.ScaleAspectFill 

          cell.imageOutlet.hnk_setImageFromURL(url, placeholder: UIImage(named: "placeholderImage.png"), format: nil, failure: nil, success: nil) 

         } 
        } 
       } 
      } 
     } 
    } 

    return cell 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     if self.allImagesSlideshow[collectionView.tag] != nil { 

      var arr:[String]? = self.allImagesSlideshow[collectionView.tag]! 

      if let arr = arr { 

       println("collection row: \(collectionView.tag), items:\(arr.count)") 

       return arr.count 
      } 
     } 

     return 0 
    } 

我設置正確的圖像的細胞。問題在於上述方法僅對第一個集合視圖單元格被調用。因此,當用戶滑動到下一個集合視圖單元格時,會再次調用上述方法,但下載圖像時會出現延遲。我希望加載的所有集合視圖單元格都能夠洞察每個可見的表格視圖單元格,而不僅僅是第一個。

使用我發佈的圖像時,每次都會加載「收集視圖單元(編號0)」,但僅當用戶滑動時才加載「收集視圖單元(編號1)」。如何強制爲集合視圖的每個單元格調用上述方法,而不僅僅是可見的方法?我想在刷用戶之前開始下載過程。

謝謝!

enter image description here

+0

請添加更多的代碼,我們可以幫助你 –

+0

我加入了更多的代碼。感謝您的建議。 – Milen

回答

1

你是對的。功能func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell將僅在單元開始出現時被調用。這是蘋果稱爲「延遲加載」的解決方案。想象你的表/集合視圖有數千行,並且所有這些init在同一時間,對於內存和處理器來說都非常糟糕。所以蘋果決定init只查看需要顯示的內容。

,並加載圖像,你可以使用一些異步加載像

https://github.com/rs/SDWebImage

它的強大,太有用:d

+0

其實我使用的是HanekeSwift,它可以下載和緩存圖像並且工作得很好。關於「延遲加載」,它確實應該像那樣工作,但是當我的表視圖單元格可見時,我應該開始下載集合視圖單元格的所有圖像,以洞察此單元格,否則當用戶刷卡時,他會看到佔位符很長時間,不好的經歷 – Milen

+0

@Milen你是怎麼做到的? 我遇到了同樣的問題 –

相關問題