這裏是代碼。這是在我的UICollectionViewDataSource
類中。collectionview只下載cellForItemAtIndexPath中的一個圖像
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier = "UICollectionViewCell"
let photo = photos[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! PhotoCollectionViewCell
let url = ImageUploaderAPI.urlForPhotoPath(photoTitle: photo.remoteURL)
if (photo.image == nil) {
Alamofire.download(url).downloadProgress { progress in
print("Download Progress: \(progress.fractionCompleted)")
}
.responseData { response in
if let data = response.result.value {
let image = UIImage(data: data)
photo.image = image!
cell.updateWithImage(image: image)
print("Downloaded: " + url.absoluteString)
collectionView.reloadData()
}
}
} else {
cell.updateWithImage(image: photo.image)
}
return cell
}
的progress.fractionCompleted
是顯示了正在下載圖像,但我不知道爲什麼沒有任何圖像正在更新。這是因爲Alamofire如何異步工作?任何意見,將不勝感激。
嘿,謝謝你的詳細解答。我現在意識到,實際上只有一個圖像被下載,其餘的只是被下載,但'responseData'回調從不被調用。有任何想法嗎? –