【问题标题】:Fetch Parse Data based on Array基于数组获取解析数据
【发布时间】:2015-09-12 20:48:34
【问题描述】:

我有一个 PFQueryTableViewController。

我还有一个随机生成的数字数组,从 1 到 Parse 类计数。

在 Parse 中,类中的每个对象都分配有一个数字(1、2、3 等)

我想在 Parse 中从这个类中获取对象到表格视图单元格,并且顺序取决于数组的顺序。

例如,我的数组是 [3, 2, 5, 1, 4]

我想查询键“数字”在哪里等于这些数字,按顺序..

到目前为止,我有这个:

// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery{

    var query = PFQuery(className: "Stores")

    for i in 1...10 {
    query.whereKey("number", equalTo: numArray[i])
    }

    return query
 }

//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ExploreCell!
    if cell == nil {
        cell = ExploreCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    }

但是,每当我运行时,它只会从 1 个对象中获取信息。我的表格视图只有 1 个抓取数据的单元格。

【问题讨论】:

    标签: arrays for-loop parse-platform tableview pfquery


    【解决方案1】:

    我想那是 Swift 代码(我其实不知道 Swift)。

    无论如何,我认为不需要(尝试)按照您想要的顺序从数组中查询对象。实际上使用query.whereKey 更多次,您将只使用最后一个。

    您应该做的实际上是查询对象然后对它们进行排序(除非您想要升序/降序)。

    另外请记住,Parse Query 是异步的,因此可能无法像您希望的那样构建函数。

    编辑:这是您可能想要使用的代码:

    var query = PFQuery(className:"GameScore")
    query.whereKey("playerName", equalTo:"Sean Plott")
    query.findObjectsInBackgroundWithBlock {
      (objects: [AnyObject]?, error: NSError?) -> Void in
    
      if error == nil {
        // The find succeeded.
        println("Successfully retrieved \(objects!.count) scores.")
        // Do something with the found objects
        if let objects = objects as? [PFObject] {
          for object in objects {
            println(object.objectId)
          }
        } 
    } else {
        // Log details of the failure
        println("Error: \(error!) \(error!.userInfo!)")
      }
    }
    

    objects 变量是您要排序的数组。

    Parse Documentation 一直有助于我更好地理解 Parse

    【讨论】:

    • 好点,我意识到它只需要最后一个。现在我正在尝试弄清楚如何将对象附加到数组中,并对其进行洗牌。你有机会知道我该如何开始吗?
    • 我编辑了答案。这是一个如何通过查询实际获取对象数组的示例。得到数组后,对它进行排序应该很容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多