簡體   English   中英

如何使用ObjectIds的解析數組填充UICollectionView?

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

我正在使用Parse.com。 我有一個objectIds數組(名稱為“ Wants”),我想用它來填充每個ObjectId的對應圖像的UICollectionView。我不太確定從哪里開始。

我嘗試使用帶有“ 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!)")
            }
        }

集合在這里查看代碼:

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
}

是的,你可以但是功能

// 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
}

是異步的,因此您的collectionView函數返回一個空單元格。 最簡單的方法是下載所有數據,而不僅僅是更新收藏夾視圖

代替使用whereKey(key:equal:To),使用whereKeyExists(key)。 這將返回所有具有Wants數組的對象。

downloadCards.whereKeyExists("Wants")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM