单元问题的PFQueryTableViewController文本



首先,让我解释一下我要做什么。我有三个类:User、Show和ShowAssignments。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,而不是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
    }

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

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
    }

看起来你已经很接近了。但是,您还没有正确地实现最后一个方法。如果出列方法实际返回的对象不是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;
}

我用同样的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
}

最新更新