如何加载我的UICollectionView?

时间:2015-05-22 13:29:47

标签: ios swift parse-platform uicollectionview

我有一个UICollectionViewUIImage填充,根据哪个按钮被点按以进入UICollectionView。无论点击哪个按钮,都会通过Parse.com查询填充UICollectionView

当我将查询代码放在按钮代码中时,查询完成,但UICollectionView为空。出于某种原因,按钮中的代码对UICollectionView几乎没有影响。如何让我的图像出现?

另一个注意事项:当我第一次点击按钮时,查询完成,但图像不会出现。但是当我点击“返回”并再次粘贴按钮时,图像都会出现。

查询代码:

var images = [UIImage]()
var parseObjects = [PFFile]()
var imageNames = [String]()
var imageExpansions = [String]()

class selectExpansionViewController: UIViewController {

@IBAction func xy1Button(sender: AnyObject) {
}

@IBAction func xy2Button(sender: AnyObject) {

    var downloadCards = PFQuery(className: "Cards")
    //downloadCards.whereKey("Expansion", equalTo:"XY2")
    downloadCards.limit = 200
    downloadCards.orderByAscending("Number")
    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["Expansion"] as! String)
                }
            }
        } else {
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }

    self.performSegueWithIdentifier("jumpToCollectionView", sender: self)

}

UICollectionView代码:

class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

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
}

}

可能会有所帮助: 当我使用所有按钮将查询代码放入视图viewDidLoad时,我可以点击一个按钮,UICollectionView可以正常工作。唯一的问题是我无法过滤掉我不想要的图像,除非我点击按钮。我遇到了同样的问题:按钮内的所有代码都对UICollectionView没有影响。

1 个答案:

答案 0 :(得分:0)

想出来。我需要将我的查询代码放在UICollectionView的viewDidLoad方法中。还需要创建每个按钮可以更改的变量,以确定查询了哪些图像。