ios - PFQueryTableViewController 中附加了重复值

标签 ios arrays swift parse-platform

所以我正在使用 Swift 2 和 Xcode 7 创建一个应用程序,并使用 Parse 作为我的后端服务。 我有两个 View Controller ,一个用于显示 PFObject 列表的 PFQueryTableViewController ,另一个用于显示所选单元格的详细信息。 我认为实现此目的的方法是将唯一的对象 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
}

最佳答案

由于您使用的是 PFQueryTableViewController,因此无需创建自己的 objectId 列表。

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

如果您需要获取选定的对象,并转到详细 View Controller ,实际上您甚至不需要使用 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)
    }
}

关于ios - PFQueryTableViewController 中附加了重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150891/

相关文章:

ios - 播放 youtube 视频 iOS 7 时 MPMoviePlayerController 错误 _itemFailedToPlayToEnd

javascript - 2 个 php 数组合并为一个 2d javascript 数组

c++ - 我需要 C++ 数组类模板,它是固定大小的、基于堆栈的并且不需要默认构造函数

python - 什么 ctypes 参数类型用于可变修改的多维数组

ios - 我正在尝试在我的应用程序中实现搜索栏,以便我可以搜索从 parse.com 检索到的对象

iphone - 获取 UITableViewCell 文本的正确方法

ios - Xcode 项目构建失败,没有错误

ios - 我可以使用自定义 iOS UI 登录用户的 Google 云端硬盘帐户吗?

arrays - 在 Swift 中从文本文件加载数值数组

ios - 如何在应用 CATransform3D 后查找 UIView 的 X、Y 和旋转值