Ios 需要选定的tableview行才能继续与定义的用户聊天

Ios 需要选定的tableview行才能继续与定义的用户聊天,ios,swift,xcode,swift4,Ios,Swift,Xcode,Swift4,当我在tableView中搜索用户并选择所需用户时,tableView会显示恢复到初始用户的情况。这是控制器的代码。我需要选择tableview行才能继续与定义的用户聊天。提前感谢您的帮助! 这是我的密码: import UIKit import Firebase class NewMessageController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating { private func

当我在tableView中搜索用户并选择所需用户时,tableView会显示恢复到初始用户的情况。这是控制器的代码。我需要选择tableview行才能继续与定义的用户聊天。提前感谢您的帮助! 这是我的密码:

import UIKit
import Firebase

class NewMessageController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {

    private func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        filterSearchController(searchBar: searchBar)
    }

    var messagesController: MessagesController?
    let cellID = "cellId"
    var users = [User]()
    private var filtered = [User]()

    private let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.backgroundColor = UIColor(r: 243, g: 237, b: 237)
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelHandle))
        tableView.register(UserCell.self, forCellReuseIdentifier: cellID)
        fetchUsers()
        navigationController?.navigationBar.shadowImage = UIImage()
        searchController.searchBar.searchBarStyle = UISearchBarStyle.prominent
        searchController.searchBar.placeholder = "Enter the name..."
        searchController.searchBar.sizeToFit()
        searchController.searchBar.isTranslucent = true
        searchController.searchBar.backgroundImage = UIImage()
        //navigationItem.titleView = searchController.searchBar
        //searchController.searchBar.returnKeyType = UIReturnKeyType.done
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        searchController.searchResultsUpdater = self
        tableView.tableHeaderView = searchController.searchBar
        searchController.obscuresBackgroundDuringPresentation = false
        tableView.reloadData()
    }

    func filterSearchController(searchBar: UISearchBar) {
        let searchText = searchBar.text ?? ""
        filtered = users.filter { user in
            let isMatchingSearchText =    (user.name?.lowercased().contains(searchText.lowercased()))! || searchText.lowercased().count == 0
            return isMatchingSearchText
        }
        tableView.reloadData()
    }

    func fetchUsers() {
        let rootRef = Database.database().reference()
        let query = rootRef.child("users").queryOrdered(byChild: "name")
        query.observe(.value) { (snapshot) in
            for child in snapshot.children.allObjects as! [DataSnapshot] {
                if let value = child.value as? NSDictionary {
                    let user = User(value as! [String : AnyObject])
                    user.id = child.key
                    if user.id == Auth.auth().currentUser?.uid {
                        continue
                    }
                    self.users.append(user)
                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                }
            }
        }
    }

    func updateSearchResults(for searchController: UISearchController) {
        filterSearchController(searchBar: searchController.searchBar)
    }

    @objc func cancelHandle() {
        dismiss(animated: true, completion: nil)
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchController.isActive ? filtered.count : users.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! UserCell

        let user = searchController.isActive ? filtered[indexPath.row] : users[indexPath.row]

        cell.textLabel?.text = user.name
        cell.detailTextLabel?.text = user.email
        cell.backgroundColor = UIColor(r: 243, g: 237, b: 237)
        let backgroundView = UIView()
        backgroundView.backgroundColor = UIColor(r: 245, g: 193, b: 108)
        cell.selectedBackgroundView = backgroundView
        cell.textLabel?.textColor = UIColor(r: 128, g: 57, b: 0)
        cell.detailTextLabel?.textColor = UIColor(r: 170, g: 83, b: 14)
        cell.textLabel?.font = UIFont(name:"Trebuchet MS", size:18)
        cell.detailTextLabel?.font = UIFont(name:"Trebuchet MS", size:12)

        if let profileImageUrl = user.profileImageUrl {
            cell.profileImageView.loadImageUsingCacheWithURLString(urlString: profileImageUrl)
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 72
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dismiss(animated: true) {
            let user = self.filtered[indexPath.row]
            self.messagesController?.showChatControllerForUser(user)
        }
    }
}
这是为用户提供的函数
showChatControllerForUser

@objc func showChatControllerForUser(_ user: User) {
        let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
        chatLogController.user = user
        navigationController?.pushViewController(chatLogController, animated: true)
    }

为什么要在显示聊天控制器之前关闭?只需将显示控制器的代码放在didSelectRowAtIndexPath@Geru,我已经更改了它,但问题仍然存在。我想我发现了错误:searchController.isActive是一个很难依赖的变量。我记得我们在依赖搜索控制器时遇到了一些问题。如果您始终使用
filtered
数组,并在搜索控制器中没有文本的情况下使筛选数组包含所有用户,那么您可能会得到更好的服务。