我每次在viewDidLoad()上调用UICollectionView时都会重新查询UICollectionView。下载我的所有图片都需要永远。我想在应用程序启动时下载所有这些,然后根据选择的集合快速调用它们。我的代码在这里:
UICollectionView代码:
var exp = ""
class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
images = []
parseObjects = []
imageNames = []
imageExpansions = []
var downloadCards = PFQuery(className: "Cards")
downloadCards.whereKey("ExpansionNumber", equalTo:"\(exp)")
downloadCards.orderByAscending("Number")
downloadCards.limit = 200
downloadCards.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) cards.")
if let objects = objects as? [PFObject] {
for object in objects {
parseObjects.append(object["Image"] as! PFFile)
imageNames.append(object["Number"] as! String)
imageExpansions.append(object["ExpansionNumber"] as! String)
self.collectionView.reloadData()
}
}
} else {
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return parseObjects.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: CardsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CardsCollectionViewCell
parseObjects[indexPath.row].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.cardsImg.image = image
}
}
//cell.cardLabel.text = imageNames[indexPath.row]
return cell
}
}
Segue和集合标识符代码:
var images = [UIImage]()
var parseObjects = [PFFile]()
var imageNames = [String]()
var imageExpansions = [String]()
class selectExpansionViewController: UIViewController {
@IBAction func xy1Button(sender: AnyObject) {
exp = "\(sender.tag)"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
}
答案 0 :(得分:0)
这不是集合视图重用问题,而是数据管理问题。特别是,您反复下载图像而不是缓存它们。
简单的选项是使用PFImageView
来处理图像PFFile
,因为它会在下载后将图像缓存到磁盘。它并没有为您提供对该缓存的大量控制,因此您可能需要考虑自己组织缓存。
通过这种方式,您只会在viewDidLoad
期间下载对象,每个对象都有对图像文件URL的引用,然后(以您选择的任何方式)检查该URL的图像是否已经下载所以你可以简单地重复使用它。