简体   繁体   English

快速如何修复为表格视图准备转场

[英]swift how to fix prepare for segue for table view

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "toChat2" {

        guard let navController = segue.destinationViewController as? UINavigationController,
        let chatController = navController.viewControllers.first as? ChatViewController else { return }

        let indexPaths = self.tableView!.indexPathsForSelectedRows! //crashes on this line
        let indexPath = indexPaths[0] as NSIndexPath

        chatController.senderId = FIRAuth.auth()?.currentUser?.uid
        chatController.senderDisplayName = FIRAuth.auth()?.currentUser?.displayName ?? ""
        chatController.friendId = messagesArray[indexPath.row].userId
        chatController.userName = messagesArray[indexPath.row].name
    }

what's the problem?有什么问题? I tried another way.我尝试了另一种方式。 I declare two variables and tried to act from here:我声明了两个变量并试图从这里开始:

var userId:String!
var userName:String!      

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    userId = messagesArray[indexPath.row].userId
    userName = messagesArray[indexPath.row].name
    self.performSegueWithIdentifier("toChat2", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "toChat2" {

        guard let navController = segue.destinationViewController as? UINavigationController,
        let chatController = navController.viewControllers.first as? ChatViewController else { return }

        chatController.senderId = FIRAuth.auth()?.currentUser?.uid
        chatController.senderDisplayName = FIRAuth.auth()?.currentUser?.displayName ?? ""
        chatController.friendId = userId
        chatController.userName = userName
    }
}

But in this way it works but not correct, after clicking one of the items in table view without responding and only from second click it moves but with data from first click!但是以这种方式它可以工作但不正确,在单击表视图中的一个项目后没有响应并且仅从第二次单击它移动但具有第一次单击的数据! :) how to fix it? :) 如何解决?

I've done that before!我以前这样做过! You are using did DESELECT row instead of did SELECT row.您正在使用 did DESELECT 行而不是 did SELECT 行。

Either way, you should probably not force unwrap those variables in your prepare for segue.无论哪种方式,您都不应该在准备转场时强制解开这些变量。 You can use another guard statement to get the indexPathsForSelectedRows.您可以使用另一个保护语句来获取 indexPathsForSelectedRows。 That way you won't crash, at least.这样你至少不会崩溃。

That is because you are using didDeselectRow instead of didSelectRow in the second example :) .那是因为您在第二个示例中使用的是didDeselectRow而不是didSelectRow :) 。 So change tableView(didDeselectRow) to tableView(didSelectRow) and the problem with sending the wrong data should be solved.所以把tableView(didDeselectRow)改成tableView(didSelectRow),应该可以解决发送错误数据的问题。

Hope it will help you.希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM