我可以为UISearchController&创建范围栏。设置标题
resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.showsScopeBar = true
controller.searchBar.scopeButtonTitles = ["One", "two", "three"]
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
但是我如何使用范围栏进行实际排序。我目前的排序方法如下
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
tableView.reloadData()
}
答案 0 :(得分:1)
您需要添加搜索栏委托,如下所示。
class NumberTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
保存寄存器委托在视图中加载如下
resultSearchController.searchBar.delegate = self
Delegate有方法告诉你按下哪个范围栏按钮如下
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterArrayWithCharacterLength(selectedScope)
}
上的教程
答案 1 :(得分:0)
即使很晚,也可能对某人有所帮助! 假设你有一个对象Person和一个filteredPeople数组,你保留了你已经过滤的人,那么
struct Person
{
let name:String
}
func updateSearchResultsForSearchController(searchController:UISearchController)
{
//Start filtering only when user starts to write something
if searchController.searchBar.text.length > 0
{
filteredPeople.removeAll(keepCapacity: false)
self.filterContentForSearchText(searchController.searchBar.text)
self.tableView.reloadData()
}
else
{
self.filteredPeople = self.people
self.tableView.reloadData()
}
}
func filterContentForSearchText(searchText: String)
{
self.filteredPeople = self.people.filter({( person: Person) -> Bool in
//In this case we are searching for all, and the search is case insensitive
let categoryMatch = (scope == "All")
let stringMatch = person.name.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return categoryMatch && (stringMatch != nil)
})
}