问题
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
}
回答1:
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
}
回答2:
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.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(GLBarcodeItem *)object {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
//cell is guaranteed to be initialized
PFRelation *relation = [object relationForKey:@"Show"];
cell.textLabel.text = [relation valueForKey:@"Name"];
return cell;
}
My best attempt at the same Swift method.
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 = object.relationForKey("Show")
cell.textLabel.text = relation.valueForKey("Name")
return cell
}
来源:https://stackoverflow.com/questions/28573978/pfquerytableviewcontroller-text-of-cell-issue