Swift PFQueryTableViewController单元格问题文本

Swift PFQueryTableViewController单元格问题文本,swift,parse-platform,pfquery,Swift,Parse Platform,Pfquery,首先,让我解释一下我想做什么。我有三个类:用户、显示和显示作业。ShowAssignments有两个关系列,一个存储Show,另一个存储User 我有一个PFQueryTableViewController,它通过ShowAssignments表运行查询排序,查找属于当前用户的所有行。然后,我想在表的单元格中显示每个显示的名称(显示表有一个名称字段) 这是我的密码: required init(coder aDecoder: NSCoder) { super.init(coder

首先,让我解释一下我想做什么。我有三个类:用户、显示和显示作业。ShowAssignments有两个关系列,一个存储Show,另一个存储User

我有一个PFQueryTableViewController,它通过ShowAssignments表运行查询排序,查找属于当前用户的所有行。然后,我想在表的单元格中显示每个显示的名称(显示表有一个名称字段)

这是我的密码:

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
    }
这似乎是在获取正确的ShowAssignment行,但单元格中的值显示为null而不是名称

**编辑**

现在,尝试通过覆盖CellForRowatineXpath设置文本

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
}
但这似乎不是正确的方法。我是否正确使用了PFRelation

****编辑****

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
    }

看来你们很接近了。但是,您尚未正确实现最后一个方法。如果dequeue方法实际返回的对象不是nil,则永远不会设置text属性

首先,除非您正在从解析中加载图像,否则没有理由使用PFTableViewCell,因为这就是类所做的一切

其次,我建议切换到
dequeueReusableCellWithIdentifier:forIndexPath:
,它保证返回一个新的单元格实例,因此可以避免nil检查

- (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;
}
我对同样快捷方法的最佳尝试

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
}

终于开始工作了。这是我现在为那些感兴趣的人提供的解决方案

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
    }

看着他们用来显示单元格名称的代码,我开始怀疑它是否能处理关系。他们所做的只是调用
objectForKey:
,我不知道这是否能够通过关系返回数据。您可能需要覆盖
tableView:cellForRowAtIndexPath:object:
并使用
relationForKey
方法获取关系对象,然后从中提取
名称
字段。这实际上很有意义,可能是我必须要做的。我在我原来的帖子中添加了我现在正在尝试的内容,我现在在CellForRowatineXpath中遇到了更多的麻烦。你能告诉我正确的方向吗?嘿,我刚刚在我原来的帖子中添加了我现在拥有的。。。我觉得我真的很接近,但这似乎仍然不起作用。调试器显示它在我试图设置单元格标签文本的行中退出,现在表没有加载任何内容。嗯,在我看来是正确的,但无论如何我都不是解析专家。也许可以尝试将单元格类型从PFTableViewCell更改为UITableViewCell?添加了我在相同swift方法中的最佳尝试-可能有一些错误,但这是最接近我在代码中所做的操作。这迫使我使用PFTableViewCell,因为我从PFQueryTableViewController继承。。但我认为这不是问题所在。我不确定我错过了什么,我知道了。我们非常接近。我不确定我的解决方案是否是最佳解决方案,但嘿,它是有效的。。我现在就发。再次感谢你的帮助。