PFQueryTableViewController中附加的重复值

时间:2015-10-15 14:15:38

标签: ios arrays swift parse-platform

所以我用Swift 2和Xcode 7创建了一个应用程序,并使用Parse作为我的后端服务。 我有两个视图控制器,一个PFQueryTableViewController显示PFObjects列表,另一个显示所选单元格的详细信息。 我认为这样做的方法是将一个唯一的对象id附加到一个数组,然后使用didSelectRowAtIndexPath来执行segue。 但是我在这里向数组添加元素时遇到了问题。加班我追加并打印数组,它显示元素2次。所以如果正确的数组是[1,2,3,4],那么我得到的是[1,2,3,4,1,2,3,4],真的很奇怪。

var arrayOfGameId = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
    let cellIdentifier = "cell"

    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? PFTableViewCell
    if cell == nil {
        cell = PFTableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
    }
    if let object = object {
        cell!.textLabel?.text = object["Title"] as? String
        cell!.detailTextLabel?.text = object["Platform"] as? String
        if let thumbnail = object["Image"]! as? PFFile {
            cell!.imageView!.image = UIImage(named: "game1.png")
            cell!.imageView!.file = thumbnail
        }
        let gameid = object["GameId"] as! String!
        arrayOfGameId.append(gameid)
    }
    print(arrayOfGameId)
    return cell
}

1 个答案:

答案 0 :(得分:0)

由于您使用的是PFQueryTableViewController,因此无需制作自己的objectIds列表。

PFObjects中返回的queryForTable会自动存储在名为objects的列表中。

如果您需要获取所选对象并转到详细视图控制器,您实际上甚至不需要使用didSelectRowAtIndexPath,请尝试以下操作。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using [segue destinationViewController]
    var detailsVC = segue.destinationViewController as! DetailsViewController

    // Pass the selected object to the destination view controller
    if let indexPath = self.tableView.indexPathForSelectedRow() {
        let row = Int(indexPath.row)

        // selectedObject is the PFObject to be displayed
        detailsVC.selectedObject = (objects?[row] as! PFObject)
    }
}