在Parse.com中,如何在swQuery中使用PFQueryTableViewController中的query.includeKey?

时间:2015-03-29 11:01:23

标签: ios swift parse-platform pfquery

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

Places中,我有两列,placeTextstringpointerToDetails为指针。

Details中,我有一列,detailText

我想在我已定义为placeText的同一detailText中显示CustomCellPFTableViewCell

不幸的是,在我运行代码后,我只在placeText内获得了CustomCell

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

2 个答案:

答案 0 :(得分:3)

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

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

  2. detailText中已包含的object["pointerToDetails"]列中提取数据,只需执行此操作:


  3. 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
        }
    }
    

答案 1 :(得分:1)

detailtext属性不会包含在您的对象中,因为includeKey()可能会建议,但Details指向的pointerToDetails对象将与您同时查询{{ 1}}对象。

因此,为了获得Places的值,您必须通过指针。换句话说,试试这个:

detailText