PFQueryTableViewController文本的单元格问题 [英] PFQueryTableViewController text of cell issue

查看:80
本文介绍了PFQueryTableViewController文本的单元格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

这是我的代码:

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.

This seems like it is grabbing the correct ShowAssignment rows, but the value in the cell is showing null instead of the Name.

**编辑**

现在,尝试通过覆盖cellForRowAtIndexPath来设置文本.

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
}

这似乎不是正确的方法.我是否正确使用PFRelation?

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
    }

推荐答案

最后使它正常工作.这是我现在为有兴趣的人提供的解决方案.

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
    }

这篇关于PFQueryTableViewController文本的单元格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆