我有一個UITableView
與UICollectionView
洞察每個表視圖單元格。我使用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)」。如何強制爲集合視圖的每個單元格調用上述方法,而不僅僅是可見的方法?我想在刷用戶之前開始下載過程。
謝謝!
請添加更多的代碼,我們可以幫助你 –
我加入了更多的代碼。感謝您的建議。 – Milen