【发布时间】:2015-05-11 16:37:40
【问题描述】:
我正在尝试从 Parse.com 下载一组图像到 UICollectionView。除了 reloadData() 函数外,一切正常。在应用程序中,我可以点击后退按钮并重新进入 UICollectionView 并且所有图像都在那里。我只是不知道我在使用 reloadData() 函数时做错了什么。
var images = [UIImage]()
var imageFiles = [PFFile]()
class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@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) cards.")
if let objects = objects as? [PFObject] {
for object in objects {
imageFiles.append(object["Image"] as! PFFile)
dispatch_async(dispatch_get_main_queue() ^{
self.collectionView.reloadData()
});
//ERROR BREAKPOINT HERE:'Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)'
}
}
} else {
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
override func viewDidLoad() {
super.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
//cell.cardsImg.image = UIImage(named: cards[indexPath.row] as String)
//return cell
imageFiles[indexPath.row].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.cardsImg.image = image
}
}
return cell
}
}
【问题讨论】:
-
你在哪里定义
self.collectionView? -
像 sahara 说的那样在 main_queue 中运行它 :) 这并不总是很直观,但通常当 UI 更改不更新或需要很长时间才能更新时,然后尝试在主队列中运行代码
-
您似乎没有
collectionView的出口,因此self.collectionView没有这样的属性。您定义的委托函数不需要一个,因为它是作为参数传入的,但要调用 reloadData(),您需要。 -
是的,您可能使用了 Nib 或 Storyboard,请确保您的 IBOutlet collectionView 已链接到 Nib/Storyboard 文件。并在主线程上重新加载
-
CollectionCollectionViewController 可能应该是 UICollectionViewController 的子类,而不是 UIViewController。
标签: ios swift parse-platform uicollectionview