Swift 2:UICollectionView - 是否可以异步 return 单元格?
Swift 2: UICollectionView - is it possible to return cell asynchronously?
我在 UIView 容器中有一个 UICollectionView
,我用它来显示附加到存储在 [=26= 中的 PFObject
的图像(作为 PFFile
的数组) ]解析。我知道我可以在我当前的实现中同步获取 PFFile/image(参见下面同步加载的工作代码),但我真的很想让它成为一个异步过程。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("attachedImageCell", forIndexPath: indexPath) as! AttachedImageCollectionViewCell
if let uploadImageFile = attachedImageFiles[indexPath.row] as? PFFile {
do {
var imageData: NSData
try imageData = uploadImageFile.getData()
cell.imageView.image = UIImage(data: imageData)
} catch let err as NSError {
print("error getting image file data: \(err)")
}
/*
//asynchronously getting image data - don't know how to return cell here
uploadImageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error != nil {
//TODO: show error in download
}
if imageData != nil {
cell.imageView.image = UIImage(data: imageData!)
}
})
*/
}
return cell
}
我一直在考虑的一种方法是让 AttachedImageCollectionViewCell
对象观察其 UIImageView
,一旦将 UIImage 文件(指针)分配给它,它将去获取 PFFile
从 Parse 并将其解析为 NSData
,用于 UIImage。
由于我仍处于掌握 Swift 的学习曲线上,我不确定这种方法的可行性。 所以我会洗耳恭听,谢谢!
因此,在创建单元格的代码中,确保使用库将图像异步设置为单元格-
https://github.com/natelyman/SwiftImageLoader/blob/master/ImageLoader.swift
//Create cell in usual way
//Code here to create the cell
//Load the image needed by the cell asynchronously
ImageLoader.sharedLoader.imageForUrl(urlString, completionHandler:{(image: UIImage?, url: String) in
cell.image = image
})
HTH
我在 UIView 容器中有一个 UICollectionView
,我用它来显示附加到存储在 [=26= 中的 PFObject
的图像(作为 PFFile
的数组) ]解析。我知道我可以在我当前的实现中同步获取 PFFile/image(参见下面同步加载的工作代码),但我真的很想让它成为一个异步过程。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("attachedImageCell", forIndexPath: indexPath) as! AttachedImageCollectionViewCell
if let uploadImageFile = attachedImageFiles[indexPath.row] as? PFFile {
do {
var imageData: NSData
try imageData = uploadImageFile.getData()
cell.imageView.image = UIImage(data: imageData)
} catch let err as NSError {
print("error getting image file data: \(err)")
}
/*
//asynchronously getting image data - don't know how to return cell here
uploadImageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error != nil {
//TODO: show error in download
}
if imageData != nil {
cell.imageView.image = UIImage(data: imageData!)
}
})
*/
}
return cell
}
我一直在考虑的一种方法是让 AttachedImageCollectionViewCell
对象观察其 UIImageView
,一旦将 UIImage 文件(指针)分配给它,它将去获取 PFFile
从 Parse 并将其解析为 NSData
,用于 UIImage。
由于我仍处于掌握 Swift 的学习曲线上,我不确定这种方法的可行性。 所以我会洗耳恭听,谢谢!
因此,在创建单元格的代码中,确保使用库将图像异步设置为单元格-
https://github.com/natelyman/SwiftImageLoader/blob/master/ImageLoader.swift
//Create cell in usual way
//Code here to create the cell
//Load the image needed by the cell asynchronously
ImageLoader.sharedLoader.imageForUrl(urlString, completionHandler:{(image: UIImage?, url: String) in
cell.image = image
})
HTH