2015-11-02 25 views
0

我有一個tableView,當單元格被按下時觸發一個segue到一個詳細視圖。該表包含當前用戶與其朋友的用戶列表。按下單元格加載該用戶的視圖(名稱,配置文件等)。這是我創建的一個非常簡單的應用程序,用於學習如何編程。Segue沒有在Swift中發送正確的單元格信息

問題是,當我按下單元格時,它總是加載表中最後一個用戶的用戶信息(並且恰好是用戶所做的最近的「朋友」)。我有一種感覺,這個問題是一個if語句我在的tableView功能:

extension MatchesViewController: UITableViewDataSource 
{ 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return numberOfMatches 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("MatchCell", forIndexPath: indexPath) 

     if PFUser.currentUser()!.objectId == self.user1.objectId{ 
      let user = matchesResults[indexPath.row]["user2"] as! PFUser 
      cell.textLabel?.text = user["first_name"] as! String 
      self.viewUser = user 
     } 

     if PFUser.currentUser()!.objectId == self.user2.objectId{ 
      let user = matchesResults[indexPath.row]["user1"] as! PFUser 
      cell.textLabel?.text = user["first_name"] as! String 
      self.viewUser = user 
     } 

     return cell 
    } 

    } 

這裏的SEGUE代碼,但我不認爲這是一個與它的問題(雖然我可能是錯的) :

extension MatchesViewController: UITableViewDelegate 
{ 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     self.performSegueWithIdentifier("UserSegue", sender: "viewUser") 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if (segue.identifier == "UserSegue") { 
      let destinationVC = segue.destinationViewController as! UserViewController 
      destinationVC.user = self.viewUser 
     } 
    } 

    } 

任何想法?如果我的tableView If語句存在問題,我該如何解決它?

謝謝!

回答

1

您可以通過indexPath得到user對象,比使其通過performSegueWithIdentifier方法的sender參數

extension MatchesViewController: UITableViewDelegate 
{ 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     let user:PFUser? 
     if PFUser.currentUser()!.objectId == self.user1.objectId{ 
      user = matchesResults[indexPath.row]["user2"] as! PFUser 
     } 

     if PFUser.currentUser()!.objectId == self.user2.objectId{ 
      user = matchesResults[indexPath.row]["user1"] as! PFUser 
     } 
     self.performSegueWithIdentifier("UserSegue", sender: user) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // sender is the user object 
     if (segue.identifier == "UserSegue") { 
      let destinationVC = segue.destinationViewController as! UserViewController 
      destinationVC.user = sender // maybe you need cast sender to UserObject 
     } 
    } 

} 
+0

感謝您的答覆!我得到'致命錯誤:意外地在'let user = cell!.tag'這一行上解開一個可選值時發現了nil。 – winston

+0

爲什麼'cell'nil?你選擇一個單元格,我認爲你必須得到選中的單元格。 – t4nhpt

+0

我把'let cell'改回了'let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(「MatchCell」,forIndexPath:indexPath)''但我保留了其他解決方案,它似乎讓我更進一步。當我加載tableView時,由於某種原因,XCode陷入用戶View而不按下單元格,並在用戶名標籤上顯示nil錯誤 – winston