简体   繁体   English

如何从解析中过滤数据并在collectionview中显示

[英]How to filter data from Parse and display in a collectionview

I'm trying to populate a collectionview with different data depending on a button that the user taps. 我试图根据用户点击的按钮用不同的数据填充collectionview。 When I run this though, it throws an error: "[PFObject whereKey:equalTo:]: unrecognized selector sent to instance". 但是,当我运行此命令时,它将引发错误:“ [[PFObject whereKey:equalTo:]:无法识别的选择器已发送到实例”。 How can I filter this data and get it to show correctly? 如何过滤此数据并使其正确显示? Thanks in advance! 提前致谢!

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()
      }

You are close in setting up a query on Parse. 您即将在Parse上设置查询。

Here's an example adapted to your problem. 这是适合您问题的示例。

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)")
    }
}

You can store the results of the query in an array that can be used by the data source of your collection view. 您可以将查询结果存储在一个数组中,供您的集合视图的数据源使用。

The query needs to complete before you show your collection view or else you won't have any data to display. 在显示集合视图之前,需要先完成查询,否则将没有任何数据可显示。

暂无
暂无

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

相关问题 从firestore中读取数据并显示在collectionView中 - Read data from firestore and display in collectionView 如何从相关表中过滤数据(解析) - how to filter data from related table (Parse) tableViewCell数据显示中的CollectionView - CollectionView in tableViewCell data display 如何让我的 CollectionView 单元格从我的 TableView 中过滤数据? - How do I make my CollectionView Cells filter data from my TableView? 单击tableview header按钮时,如何将tableviewcell内的collectionview中的数据显示到另一个视图controller - How to display data from collectionview inside tableviewcell to another view controller when tableview header button is clicked 如何基于Firestore数据在collectionView中显示行标题和节标题? - How to display row and section titles in collectionView based on Firestore data? 如何在 iPhone 的 list(tableView) 和 iPad 的 grid(collectionView) 上显示数据 - How to display the data on list(tableView) for iPhone and grid(collectionView) for iPad 将数据从一个 collectionView 传递到另一个 collectionView - Passing data from one collectionView to other collectionView 使用 NotificationCenter 将数据从 CollectionView 发送到 CollectionView 2 - Sending data from CollectionView to CollectionView two with NotificationCenter 如何将图像从CollectionView传输/显示到另一个ViewController - How to transfer/display an image from CollectionView to another ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM