【发布时间】:2015-05-14 16:22:04
【问题描述】:
每次调用 UICollectionView 时,我都需要重新加载图像。我目前有两个按钮(还有更多按钮),它们通过 Parse.com 查询连接并填充 UICollectionView。当我单击我的按钮时,图像被下载,并且 UICollectionView 有一个 segue,但图像仅在我点击“返回”并重新进入 UICollectionView 时显示。我试过使用 self.collectionView.reloadData() 但我只得到一个错误。
var images = [UIImage]()
var imageFiles = [PFFile]()
class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
@IBAction func xy1Button(sender: AnyObject) {
}
@IBAction func xy2Button(sender: AnyObject) {
var downloadImages = PFQuery(className: "XY2")
downloadImages.whereKey("Expansion", equalTo: "XY2")
downloadImages.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) images.")
if let objects = objects as? [PFObject] {
for object in objects {
imageFiles.append(object["Image"] as! PFFile)
}
}
} else {
println("Error: \(error!) \(error!.userInfo!)")
}
}
self.collectionView.reloadData()
//ERROR IS ABOVE: "Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
override func viewDidLoad() {
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageFiles.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: CardsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CardsCollectionViewCell
imageFiles[indexPath.row].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.Img.image = image
}
}
return cell
}
}
有什么帮助吗?!
【问题讨论】:
-
collectionView 第一次加载时是一个有效的对象吗(你确定它不为空)吗?
-
这就是问题所在。第一次加载时为空。
-
对了,需要初始化,有什么问题?
标签: ios swift parse-platform uicollectionview