Search code examples
swiftparse-platformpfquery

PFQueryTableViewController text of cell issue


First, let me explain what it is that I am trying to do. I have three classes: User, Show and ShowAssignments. ShowAssignments has two relational columns, one that stores a Show and one that stores a User.

I have a PFQueryTableViewController that runs a query sorting through the ShowAssignments table looking for all rows that belong to the current user. Then, I want to display the Name of each Show (the Show table has a Name field) in the cells of the table.

Here is my code:

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

        self.parseClassName = "ShowAssignments"
        self.textKey = "Show.Name"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
    }

    override func queryForTable() -> PFQuery! {
        var query = PFQuery(className: "ShowAssignments")
        query.whereKey("User", equalTo: PFUser.currentUser())
        query.includeKey("Show")

        return query
    }

This seems like it is grabbing the correct ShowAssignment rows, but the value in the cell is showing null instead of the Name.

** edit **

Now, trying this to set the text by overriding cellForRowAtIndexPath..

override func tableView(tableView: UITableView!, cellForRowAtIndexPath         indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let CellIdentifier: String = "myShowCell"

        var cell =     tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as?     PFTableViewCell

        if cell != nil
        {
            var obj: PFObject! = self.objectAtIndexPath(indexPath)
            var show: PFRelation! = obj.relationForKey("Show")

            cell?.textLabel?.text = show.valueForKey("Name") as? String
        }

        return cell
}

This doesn't seem to be the right way to do it though. Am I using PFRelation correctly?

**** edit ****

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

        var cellIdentifier: String = "myShowCell"

        var cell =     tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:     indexPath) as PFTableViewCell

        var relation: PFRelation = object.relationForKey("Show")
        cell.textLabel?.text = relation.valueForKey("Name") as String

        return cell as PFTableViewCell
    }

Solution

  • Finally got it working. Here is my solution now for those interested.

    required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            self.parseClassName = "UserShowAssignments"
            self.textKey = "Show"
            self.pullToRefreshEnabled = true
            self.paginationEnabled = false
        }
    
        override func tableView(tableView: UITableView!,     cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) ->     PFTableViewCell! {
    
            var cellIdentifier: String = "myShowCell"
    
            var cell =     tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:     indexPath) as PFTableViewCell
    
            var show: PFObject = object.objectForKey("Show") as PFObject
            cell.textLabel?.text = show.valueForKeyPath("Name") as? String
    
            return cell as PFTableViewCell
        }
    
        override func queryForTable() -> PFQuery! {
            var query = PFQuery(className: "UserShowAssignments")
            query.whereKey("User", equalTo: PFUser.currentUser())
            query.includeKey("Show")
    
            return query
        }