gpt4 book ai didi

ios - 为什么 reloadData() 在我的应用程序中不起作用?

转载 作者:搜寻专家 更新时间:2023-10-31 22:00:31 25 4
gpt4 key购买 nike

我正在尝试从 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
}
}

最佳答案

需要在主线程中执行reloadData 来更新UI。你可以试试这个

dispatch_async(dispatch_get_main_queue(), ^{
self.collectionView.reloadData()
});

正如@Grimxn 所建议的,您应该检查您的 collectionView IBOutlet 是否已连接到 Storyboard或 xib 文件中。

关于ios - 为什么 reloadData() 在我的应用程序中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30173085/

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