当我双击太快时 PushViewController 两次

富源V*_*rve 2 uitableview uinavigationcontroller ios swift

当我调用将 ViewController 推送到详细聊天控制器(一对一聊天)时,我有以下代码。但是,如果我点击得太快,视图控制器将被推送两次。我看了两次动画。谁能指出我的错误在哪里?该代码来自 LBTA 的 Youtube 课程 (Firebase Chat)。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let message = messages[indexPath.row]
    guard let chatPartnerId = message.chatPartnerId() else {return}

    let ref = Database.database().reference().child("users").child(chatPartnerId)
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? [String: AnyObject] else {
            return
        }
        let user = ChatUser(dictionary: dictionary)
        user.id = chatPartnerId
        self.showChatControllerForUser(user)

    }, withCancel: nil)

}

func showChatControllerForUser(_ user: ChatUser) {
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
    chatLogController.chatUser = user
    navigationController?.pushViewController(chatLogController, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

Ahm*_*d F 5

为避免此问题,您可以做的是禁用表视图用户交互并在推送到第二个视图控制器后重新启用它。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // add this:
    tableView.isUserInteractionEnabled = false
    let message = messages[indexPath.row]
    guard let chatPartnerId = message.chatPartnerId() else {return}

    let ref = Database.database().reference().child("users").child(chatPartnerId)
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? [String: AnyObject] else {
            return
        }
        let user = ChatUser(dictionary: dictionary)
        user.id = chatPartnerId
        self.showChatControllerForUser(user)

    }, withCancel: nil)

}

func showChatControllerForUser(_ user: ChatUser) {
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
    chatLogController.chatUser = user
    // edit this:
    navigationController?.pushViewController(chatLogController, animated: true)

    navigationController?.pushViewController(chatLogController, animated: true, completion: {
        self.tableView.isUserInteractionEnabled = true
    })
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,pushViewController(_:animated:)没有完成处理程序,因此作为一种解决方法,我们可以添加以下扩展来实现它:

extension UINavigationController {
    public func pushViewController(
        _ viewController: UIViewController,
        animated: Bool,
        completion: @escaping () -> Void)
    {
        pushViewController(viewController, animated: animated)

        guard animated, let coordinator = transitionCoordinator else {
            DispatchQueue.main.async { completion() }
            return
        }

        coordinator.animate(alongsideTransition: nil) { _ in completion() }
    }
}
Run Code Online (Sandbox Code Playgroud)

引自:https : //stackoverflow.com/a/33767837/5501940