如何在 UISearchController 中使用范围栏
How to use scope bar with UISearchController
我能够为 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()
}
虽然迟到了,但也许能帮到别人!
假设您有一个对象 Person 和一个 filteredPeople 数组,您在其中保存已过滤的 People,那么
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)
})
}
您需要按如下方式添加搜索栏委托。
class NumberTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
在视图中保存注册委托确实加载如下
resultSearchController.searchBar.delegate = self
Delegate 有方法告诉您按下了哪个范围栏按钮,如下所示
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterArrayWithCharacterLength(selectedScope)
}
有关详细信息,请查看我在 How to add UISearchController to tableView
上的教程
我能够为 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()
}
虽然迟到了,但也许能帮到别人! 假设您有一个对象 Person 和一个 filteredPeople 数组,您在其中保存已过滤的 People,那么
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)
})
}
您需要按如下方式添加搜索栏委托。
class NumberTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
在视图中保存注册委托确实加载如下
resultSearchController.searchBar.delegate = self
Delegate 有方法告诉您按下了哪个范围栏按钮,如下所示
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterArrayWithCharacterLength(selectedScope)
}
有关详细信息,请查看我在 How to add UISearchController to tableView
上的教程