2015-06-01 29 views
0

我正在使用Parse.com。我有一個數組(名爲「想要」objectIds,我想用它來填充每個objectId的相應圖像的UICollectionView。我不知道從哪裏開始。如何使用objectIds的Parse數組填充UICollectionView?

我試着使用一個PFQuery與Wherekey的「想要」,但我不知道要放什麼東西在‘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 
} 

回答

0

當然可以,但功能

// 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函數返回一個空單元格。最簡單的方法是下載所有數據,然後更新您的集合視圖

0

而不是使用whereKey(key:equal:To)使用whereKeyExists(key)。這將返回具有想要數組的所有對象。

downloadCards.whereKeyExists("Wants")