【问题标题】:in Parse.com, how to use query.includeKey in PFQueryTableViewController with swift?在 Parse.com 中,如何快速使用 PFQueryTableViewController 中的 query.includeKey?
【发布时间】:2015-03-29 11:01:23
【问题描述】:

我正在尝试使用 PFQueryTableViewController 填充表。 我想使用来自两个类的数据,即PlacesDetails

Places 中,我有两列,placeText 作为stringpointerToDetails 作为指针。

Details 中,我有一列,即detailText

我想在同一个CustomCell 中显示placeTextdetailText,我已经定义为PFTableViewCell

不幸的是,在我运行代码之后,我只得到了CustomCell 中的placeText

import UIKit

class TableViewController: PFQueryTableViewController {

    // Initialise the PFQueryTable tableview
    override init!(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Configure the PFQueryTableView
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
    }

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

        var query = PFQuery(className: "Places")
        query.includeKey("pointerToDetails")
        return query
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell!
        if cell == nil {
            cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
        }

        // Extract values from the PFObject to display in the table cell
        cell.name.text = object["placeText"] as String!
        cell.detail.text = object["detailText"] as String!

        return cell
    }
}

【问题讨论】:

    标签: ios swift parse-platform pfquery


    【解决方案1】:

    在我从@deadbeef 获得灵感后(见答案 1),这是我得到的解决方案:

    1. query.includeKey("pointerToDetails") 正在查询可以通过object["pointerToDetails"] 访问的对象。

    2. 要从detailText 列中提取已包含在object["pointerToDetails"] 中的数据,只需这样做:


    if let pointer = object["pointerToDetails"] as? PFObject {
         cell.detail.text = object["detailText"] as String!
    }
    

    这是整个代码:

    import UIKit
    
    class TableViewController: PFQueryTableViewController {
    
        // Initialise the PFQueryTable tableview
        override init!(style: UITableViewStyle, className: String!) {
            super.init(style: style, className: className)
        }
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            // Configure the PFQueryTableView
            self.pullToRefreshEnabled = true
            self.paginationEnabled = false
        }
    
        // Define the query that will provide the data for the table view
        override func queryForTable() -> PFQuery! {
    
            var query = PFQuery(className: "Places")
            query.includeKey("pointerToDetails")
            return query
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
            var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell!
            if cell == nil {
                cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
            }
    
            // Extract values from the PFObject to display in the table cell
            cell.name.text = object["placeText"] as String!
            if let pointer = object["pointerToDetails"] as? PFObject {
                cell.detail.text = object["detailText"] as String!
            }
            return cell
        }
    }
    

    【讨论】:

      【解决方案2】:

      detailtext 属性不会像includeKey() 建议的那样包含在您的对象中,但pointerToDetails 指向的Details 对象将与Places 对象一起被查询。

      所以为了得到detailText的值,你必须通过指针。换句话说,试试这个:

      cell.name.text = object["placeText"] as String!
      cell.detail.text = object["pointerToDetails"]["detailText"] as String!
      

      【讨论】:

      • 感谢您的想法。实际上,你的不适合我。但我发现这是有效的。你可以在上面看到我的答案。 if let pointer = object["pointerToDetails"] as? PFObject { cell.detail.text = object["detailText"] as String! }
      • 是的,这看起来更快捷。对不起,我实际上不知道 swift,我只是在猜测语法 ^^
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多