Segue 未在 Swift 中发送正确的单元格信息

Posted

技术标签:

【中文标题】Segue 未在 Swift 中发送正确的单元格信息【英文标题】:Segue not sending correct cell info in Swift 【发布时间】:2015-11-02 02:46:35 【问题描述】:

我有一个 tableView,当按下一个单元格时,它会触发一个详细视图的 segue。该表包含与 currentUser 为朋友的用户列表。按下单元格会加载该用户的视图(姓名、个人资料等)。这是我正在创建的一个非常简单的应用程序,用于学习如何编程。

问题是当我按下一个单元格时,它总是加载表格中最后一个用户的用户信息(并且恰好是用户最近的“朋友”)。我感觉问题出在 tableView 函数中的 if 语句上:

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
        
    


【讨论】:

感谢您的回复!我得到fatal error: unexpectedly found nil while unwrapping an Optional value 在线let user = cell!.tag 为什么cell 为零?你选择一个单元格,我认为你必须得到选中的单元格。 我将let cell 改回let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("MatchCell", forIndexPath: indexPath),但我保留了您解决方案的其他行,这似乎让我更进一步。当我加载 tableView 时,XCode 出于某种原因在不按下单元格的情况下转到用户视图,并在用户名标签上显示 nil 错误 查看此内容以获取所选单元格:***.com/questions/26158768/… 我想我很接近了。当我单击一个单元格时,我收到一个错误Could not cast value of type '__NSCFNumber' (0x110a60130) to 'PFUser' (0x10f41ccc0). 它在destinationVC.user = sender as! PFUser 行上出错。出于某种原因,它不喜欢发件人。

以上是关于Segue 未在 Swift 中发送正确的单元格信息的主要内容,如果未能解决你的问题,请参考以下文章

Swift - Segue 没有被传递正确的数据

单元格的 detailTextLabel 未在 swift 3 中显示

从 App Delegate (Swift) 调用 ViewControllers 方法或 Segue

在 Swift 中 segue 后从堆栈中删除(删除)视图/视图控制器的正确方法

如果 segue.identifier == "showChildFiles" 当 segue 标识符看起来正确时,Swift 3 准备 segue 不通过

Facebook 登录后,Swift 项目没有正确地进行 segue