简体   繁体   English

如何使用ObjectIds的解析数组填充UICollectionView?

[英]How can I populate a UICollectionView with a Parse array of objectIds?

I am using Parse.com. 我正在使用Parse.com。 I have an array(named "Wants" of objectIds that I want to use to populate a UICollectionView with each objectId's corresponding image. I'm not quite sure where to start. 我有一个objectIds数组(名称为“ Wants”),我想用它来填充每个ObjectId的对应图像的UICollectionView。我不太确定从哪里开始。

I tried using a PFQuery with a Wherekey of "Wants", but I'm not sure what to put in the "equalTo" parameter. 我尝试使用带有“ Wants”的Wherekey的PFQuery,但不确定“ equalTo”参数中应包含什么。

        var downloadCards = PFQuery(className: "User")
        downloadCards.whereKey("Wants", equalTo:"")
        downloadCards.orderByAscending("Number")
        downloadCards.limit = 200
        downloadCards.findObjectsInBackgroundWithBlock {

            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        objectIds.append(object.objectId!)
                        parseObjects.append(object["Image"] as! PFFile)
                        imageNames.append(object["Number"] as! String)
                        imageExpansions.append(object["ExpansionNumber"] as! String)
                        self.collectionView.reloadData()
                    }
                }
            } else {
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }

Collection View code here: 集合在这里查看代码:

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

        }   

    }
    return cell
}

Yes you can but the function 是的,你可以但是功能

// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryTableView
    self.parseClassName = "yourClass"

    self.textKey = "yourObject"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

Is async so your collectionView function is returning an empty cell. 是异步的,因此您的collectionView函数返回一个空单元格。 The easiest way around is to download all the data and just than update your collection view 最简单的方法是下载所有数据,而不仅仅是更新收藏夹视图

Instead of using whereKey(key:equal:To) use whereKeyExists(key). 代替使用whereKey(key:equal:To),使用whereKeyExists(key)。 This will return all objects that have a Wants array. 这将返回所有具有Wants数组的对象。

downloadCards.whereKeyExists("Wants")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM