2015-11-02 63 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

相关问题