PFQueryTableViewController单元格问题的文本

时间:2015-02-18 00:43:18

标签: swift parse-platform pfquery

首先,让我解释一下我想要做什么。我有三个类:User,Show和ShowAssignments。 ShowAssignments有两个关系列,一个存储一个Show,另一个存储User。

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

这是我的代码:

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

**编辑**

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

2 个答案:

答案 0 :(得分:1)

终于搞定了。现在,对于那些感兴趣的人,这是我的解决方案。

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
    }

答案 1 :(得分:0)

看起来你非常接近。但是,您还没有正确实现最后一种方法。如果dequeue方法实际返回的语句不是nil,则永远不要设置text属性。

首先,没有理由使用PFTableViewCell,除非您从解析中进行图像加载,因为这就是该类的所有内容。

其次,我建议切换到dequeueReusableCellWithIdentifier: forIndexPath:,保证返回一个新的单元格实例,这样就可以避免零检查。

- (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
}