将搜索栏添加到现有标题Swift 3

时间:2017-01-15 18:35:57

标签: ios swift3 uisearchcontroller

我根据Ray Wenderlich教程(https://www.raywenderlich.com/113772/uisearchcontroller-tutorial)将搜索框和搜索标签栏添加到我的Swift 3项目中

这适用于大多数表格。但是,我的一个表有一个已在故事板中定义的标题。如果我使用如下所示的代码,它将替换故事板中定义的标题,并且还有一些其他副作用。

因此,我想知道如何执行以下操作之一:

a)使用代码将搜索栏附加到现有标题 b)在故事板中添加一个搜索栏对象,然后正确设置插座,以便我的其他搜索代码引用它。

我有以下代码

在didload中

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["All", "Active", "Not Active"]
searchController.searchBar.delegate = self
tableView.tableHeaderView = searchController.searchBar

主要课程以上的扩展

extension ItemsViewController: UISearchResultsUpdating{
    func updateSearchResults(for: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }
}

extension ItemsViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        //filterContentForSearchText(searchText: searchController.searchBar.text!)
        filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
    }
}

过滤代码

    func filterContentForSearchText(searchText: String, scope: String = 
... filter logic ..     
tableView.reloadData()
    }

任何想法都会非常感激

0 个答案:

没有答案