搜索Bar Glitch

时间:2017-06-30 20:07:06

标签: ios swift uitableview delegates uisearchbar

问题:

我没有将搜索栏(带有搜索和结果控制器)添加到表视图控制器,而是将其添加到常规视图控制器的导航栏中。起初一切似乎都很好,但当我点击搜索栏时,屏幕变为灰色。

这是我的代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchBarDelegate{

    var schools = ["Saratoga", "Fremont", "Argonaut", "Redwood", "Foothill", "Miller", "Rolling Hills"].sorted()
    var filteredSchools = ["Saratoga", "Fremont", "Argonaut", "Redwood", "Foothill", "Miller", "Rolling Hills"].sorted()

    var searchController: UISearchController!
    var resultsController: UITableViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        resultsController = UITableViewController()
        searchController = UISearchController(searchResultsController: resultsController)

        resultsController.tableView.delegate = self
        resultsController.tableView.dataSource = self
        searchController.searchResultsUpdater = self

        self.view.addSubview(searchController.searchBar)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: searchController.searchBar)
    }

    func updateSearchResults(for searchController: UISearchController) {
        let currText = searchController.searchBar.text ?? ""
        filteredSchools = schools.filter({ (school) -> Bool in
            if school.contains(currText){
                return true
            }
            return false
        })
        resultsController.tableView.reloadData()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredSchools.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = filteredSchools[indexPath.row]
        return cell
    }

}

1 个答案:

答案 0 :(得分:1)

viewDidLoad中添加这些行:

resultsController.tableView.backgroundColor = UIColor.clear
searchController.hidesNavigationBarDuringPresentation = false

您的导航栏隐藏了所有内容。

如果您不想要灰色调:

searchController.dimsBackgroundDuringPresentation = false