[英]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.