【问题标题】:Search Bar Glitch搜索栏故障
【发布时间】:2017-06-30 20:07:06
【问题描述】:

问题:

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

这是我的代码:

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
    }

}

【问题讨论】:

标签: ios swift uitableview delegates uisearchbar


【解决方案1】:

viewDidLoad中添加这些行:

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

您的导航栏正在隐藏,仅此而已。

如果您不想要灰色调:

searchController.dimsBackgroundDuringPresentation = false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多