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
}
It looks like you're pretty close. You haven't implemented the last method correctly, however. If the dequeue method actually returns an object other than nil, you never set the text property.
First off, there is no reason to use a PFTableViewCell unless you are doing image loading from parse, as that is all that class does.
Second, I'd recommend switching to
dequeueReusableCellWithIdentifier: forIndexPath:
which is guaranteed to return a new instance of a cell, so you avoid the nil check.My best attempt at the same Swift method.
Finally got it working. Here is my solution now for those interested.