当searchBar(来自searchController)开始编辑Swift时显​​示表视图

时间:2017-10-19 14:40:53

标签: swift uitableview uisearchbar search-engine uisearchcontroller

我已经看到了这个问题:How to show tableview when user taps searchbar swift 这与我的问题相同,但是当我使用答案时,它对我来说无效。

当我点击按钮搜索时,搜索控制器上只会出现搜索栏,如果我输入了某些内容,那么tableView根本就不显示。我使用Swift 3.我想要做的最好的例子是Instagram搜索功能。当用户点击搜索栏时,tableView将作为子视图。

我向您展示我的SearchViewController的当前代码(我在Firebase中搜索用户名):

class SearchViewController: UIViewController, UISearchBarDelegate {

var tableViewController: UserTableViewController!
var searchResultController: SearchResultsController!

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func searchWithAddress(_ sender: AnyObject) {
    let searchController = UISearchController(searchResultsController: tableViewController)
    searchController.searchBar.delegate = self
    self.present(searchController, animated: true, completion: nil)
}
}

这是TableViewController的代码(当搜索栏正在编辑时,如何在SearchViewController上显示它?):

class UserTableViewController: UITableViewController, UISearchResultsUpdating {

var userTab = [String]()
var filteredUsers = [String]()

var searchController: UISearchController!
var resultsController = UITableViewController()

var name: String!
var ref: DatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()

    self.resultsController.tableView.dataSource = self
    self.resultsController.tableView.delegate = self

    ref = Database.database().reference()
    ref.child("users").queryOrdered(byChild: "pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
        self.userTab = []
        if (snapshot.value is NSNull) {
            print("No data")
        } else {
            for child in snapshot.children {
                let userSnap = child as! DataSnapshot
                let uid = userSnap.key
                let userDict = userSnap.value as! [String: AnyObject]
                let pseudo = userDict["pseudo"] as! String // Find username in Firebase
                self.userTab.append(pseudo) // And put all username in an array
                self.resultsController.tableView.reloadData()
                continue
            } } })

    self.searchController = UISearchController(searchResultsController: self.resultsController)
    self.tableView.tableHeaderView = self.searchController.searchBar
    self.searchController.searchResultsUpdater = self
    self.searchController.dimsBackgroundDuringPresentation = false
    self.definesPresentationContext = true
}

func updateSearchResults(for searchController: UISearchController) {
    self.filteredUsers = self.userTab.filter { (user:String) -> Bool in
        if user.lowercased().contains(self.searchController.searchBar.text!.lowercased()) {
            return true
        } else {
            return false
        }
    }
    self.resultsController.tableView.reloadData()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "userProfil" { // For see the user profil
            let userViewController = segue.destination as! UserViewController
            userViewController.pseudo = self.name!
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.tableView {
        return self.userTab.count
    } else {
        return self.filteredUsers.count
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    if tableView == self.tableView {
        cell.textLabel?.text = self.userTab[indexPath.row]
    } else {
        cell.textLabel?.text = self.filteredUsers[indexPath.row]
    }
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let post = filteredUsers[indexPath.row] as! String
    self.name = post
    performSegue(withIdentifier: "userProfil", sender: nil)
}
}

0 个答案:

没有答案