gpt4 book ai didi

swift 2 : UICollectionView - is it possible to return cell asynchronously?

转载 作者:行者123 更新时间:2023-11-30 10:08:44 24 4
gpt4 key购买 nike

我在 UIView 容器中有一个 UICollectionView,我用它来显示附加到 PFObject 的图像(作为 PFFile 的数组) > 存储在解析中。我知道我可以在当前的实现中同步获取 PFFile/图像(请参阅下面的同步加载工作代码),但我真的很想让它成为一个异步过程。

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 文件(指针)分配给它,它就会去获取从 Parse 中获取 PFFile 并将其解析为 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

关于 swift 2 : UICollectionView - is it possible to return cell asynchronously?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34428013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com