2015-10-15 21 views
0

所以我使用Swift 2和Xcode 7創建了一個應用程序,並使用Parse作爲我的後端服務。 我有兩個視圖控制器,一個PFQueryTableViewController顯示PFObjects列表,另一個顯示選定單元格的細節。 我想這樣做的方法是將一個唯一的對象ID添加到數組,然後使用didSelectRowAtIndexPath來執行segue。 但我遇到了在這裏向數組追加元素的問題。加班我追加並打印數組,它顯示元素2次。所以如果正確的數組是[1,2,3,4],那麼我得到的是[1,2,3,4,1,2,3,4],真的很奇怪。在PFQueryTableViewController中附加重複值

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 
} 

回答

0

由於您使用的是PFQueryTableViewController,因此不需要製作自己的objectId列表。

queryForTable返回的PFObjects自動存儲在名爲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) 
    } 
} 
+0

非常感謝! – WhiteRice

+0

歡迎來到StackOverflow!如果您的問題得到解決,請注意,如果有幫助,請標記爲答案 – Russell