单元格问题的 PFQueryTableViewController 文本

PFQueryTableViewController text of cell issue

首先,让我解释一下我正在尝试做什么。我有三个 类:User、Show 和 ShowAssignments。 ShowAssignments 有两个关系列,一个存储节目,一个存储用户。

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

这是我的代码:

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 而不是名称。

** 编辑 **

现在,尝试通过覆盖 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
}

但这似乎不是正确的方法。我是否正确使用了 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 方法实际上 return 是 nil 以外的对象,则您永远不会设置文本 属性.

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

其次,我建议切换到 dequeueReusableCellWithIdentifier: forIndexPath:,这样可以保证 return 一个单元格的新实例,从而避免 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;
}

我对相同 Swift 方法的最佳尝试。

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
    }