如何从Parse中过滤数据并在集合视图中显示

时间:2015-10-22 23:38:48

标签: ios swift uicollectionview

我尝试使用不同的数据填充集合视图,具体取决于用户点按的按钮。当我运行它时,它会抛出一个错误:" [PFObject whereKey:equalTo:]:无法识别的选择器发送到实例"。如何过滤此数据并使其正确显示?提前谢谢!

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

      let cell: iconCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! iconCollectionViewCell

      if button1.selected {

            let stance = stances[indexPath.row].whereKey("stanceType", equalTo: "Character")
            iconImage = (stance.valueForKey("iconUnselected") as? PFFile)!
            cell.emojiIconPFImageView.file = iconImage
            cell.emojiIconPFImageView.loadInBackground()

      } else if button2.selected {

         let stance = stances[indexPath.row].whereKey("stanceType", equalTo: "Policy")
         iconImage = (stance.valueForKey("iconUnselected") as? PFFile)!
         cell.iconPFImageView.file = iconImage
         cell.iconPFImageView.loadInBackground()
      }

1 个答案:

答案 0 :(得分:0)

您即将在Parse上设置查询。

以下是适合您问题的示例。

let query = PFQuery(className: "MyParseClass")
query.whereKey("stanceType", equalTo:"Character")
query.findObjectsInBackgroundWithBlock {
    (objects: [PFObject]?, error: NSError?) -> Void in

    if error == nil {
        // The find succeeded.
        print("Successfully retrieved \(objects!.count) objects.")
        // Do something with the found objects
        if let objects = objects {
            for object in objects {
                print(object.objectId)
            }
        }
    } else {
        // Log details of the failure
        print("Error: \(error!) \(error!.userInfo)")
    }
}

您可以将查询结果存储在可以由集合视图的数据源使用的数组中。

查询需要在显示集合视图之前完成,否则您将无法显示任何数据。