如何显示搜索结果,例如iOS“联系人”应用

时间:2018-06-28 17:53:54

标签: ios swift uitableview uisearchbar

我是新的iOS编程。我试图将我的应用程序实现为类似于iOS联系人应用程序。但是我不知道如何实现我的愿望。我想让搜索结果看起来像默认的iOS应用。

看看我到目前为止所尝试的:

When i type something the dimsBackgroundDuringPresentation still <code>true</code>

当我键入内容时,dimsBackgroundDuringPresentation仍为true

这是我的期望:

Here is detail of my expectation

我想知道这个应用如何显示这样的结果。

这是我如何声明UISearchController

lazy var searchController: UISearchController = ({

    let controller = UISearchController(searchResultsController: nil)

    controller.hidesNavigationBarDuringPresentation = false
    controller.searchBar.sizeToFit()
    controller.searchBar.backgroundColor = UIColor.clear
    controller.searchBar.placeholder = "Search"
    controller.dimsBackgroundDuringPresentation = true

    return controller

})()

这是我将searchBar初始化为tableView的标头部分的方式

let searchBar = searchController.searchBar
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    searchBar.delegate = self

    tableView.tableHeaderView = searchController.searchBar

这里是代表UISearchResultsUpdating的功能

func updateSearchResults(for searchController: UISearchController) {
    if let count = searchController.searchBar.text?.count {
        if count > 0  {

            filterArray.removeAll(keepingCapacity: false)
            var a = [String]()
            a = [searchController.searchBar.text!]
            filterArray = a
            searchController.dimsBackgroundDuringPresentation = false
            tableView.reloadData()
        }else {
            searchController.dimsBackgroundDuringPresentation = true
            filterArray.removeAll(keepingCapacity: false)
            filterArray = array
            tableView.reloadData()
        }
    }

}

这是我的tableView单元格

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.isActive {
        return filterArray.count
    }else {
        return array.count
    }

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


    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

    if searchController.isActive {
        cell.textLabel?.text = filterArray[indexPath.row]
    }else {
        cell.textLabel?.text = array[indexPath.row]
    }


    return cell

}

2 个答案:

答案 0 :(得分:0)

尝试以下选项:

let search = UISearchController(searchResultsController: nil)
            self.definesPresentationContext = true
            search.dimsBackgroundDuringPresentation = false
            navigationItem.searchController = search
            navigationItem.hidesSearchBarWhenScrolling = false

在我的仓库中尝试代码以查看其工作原理,只需轻按一个tableview项,它将搜索到第二个控制器,您可以在其中看到搜索栏,然后轻按它以查看是否是您想要的

github link

答案 1 :(得分:0)

    if navigationItem.searchController == nil {
        if searchController == nil {
            searchController = UISearchController(searchResultsController: nil)
            searchController?.searchBar.placeholder = "Search"
            searchController?.delegate = self
            searchController?.searchResultsUpdater = self
            searchController?.dimsBackgroundDuringPresentation = false
            searchController?.searchBar.searchBarStyle = .minimal
        }
        self.navigationItem.searchController = searchController
        self.navigationItem.hidesSearchBarWhenScrolling = false
    }

使用属性 hidesSearchBarWhenScrolling 来使搜索栏不像联系人应用程序那样向上滚动。