簡體   English   中英

如何從解析中過濾數據並在collectionview中顯示

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

我試圖根據用戶點擊的按鈕用不同的數據填充collectionview。 但是,當我運行此命令時,它將引發錯誤:“ [[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()
      }

您即將在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)")
    }
}

您可以將查詢結果存儲在一個數組中,供您的集合視圖的數據源使用。

在顯示集合視圖之前,需要先完成查詢,否則將沒有任何數據可顯示。

暫無
暫無

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

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