Why is reloadData () not working in my application?

I am trying to load a collection of images from Parse.com into a UICollectionView. Everything works except for the reloadData () function. In the app, I can hit the back button and re-enter the UICollectionView and all the images are there. I just don't know what I am doing wrong with the reloadData () function.

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() ^{
                        //ERROR BREAKPOINT HERE:'Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)'
            } else {
                println("Error: \(error!) \(error!.userInfo!)")
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
    //cell.cardsImg.image = UIImage(named: cards[indexPath.row] as String)
    //return cell
        (imageData: NSData?, error: NSError?) -> Void in

        if error == nil {

            let image = UIImage(data: imageData!)

            cell.cardsImg.image = image

    return cell



source to share

1 answer

To update the UI on the main thread, you need to execute reloadData

. You can try this

dispatch_async(dispatch_get_main_queue(), ^{


And as @Grimxn suggested, you should check if your collectionView

IBOutlet is connected in storyboard or xib file.



All Articles