如何使用搜索控制器更新我的 Table 控制器过滤?
How can I update my Table Controller filtering with a Search Controller?
我是 IOS 开发的新手,所以有些事情很难理解。
在我的应用程序中,当用户打开主屏幕时,我会显示一个任务列表和一个用于过滤任务的搜索。如果用户在我的搜索控制器中输入 "Monday",它应该只过滤星期一的任务。所有代码都可以正常工作,但我不知道这样做是否正确。
我有一个 Table 视图控制器,它具有所有任务并在用户打开应用程序时显示。因此,用户首先应该看到的是他的所有任务。
当用户在 table header 中打开搜索时,它应该过滤他的任务(从第一个 table 开始)并仅显示与过滤器对应的任务。它也工作正常。
现在,我的问题。我有两个 table。一个用于主视图控制器,另一个用于搜索控制器。
我必须为删除任务编写两次获取单元格和编辑样式的代码。
我做错了什么?我怎样才能对我的主 table 进行过滤器并将所有内容保留在主 table 中,而不是将所有内容复制到第二个控制器?
谢谢
取一个 Bool var searchActive = false
和一个空数组 var filteredItems = [NSDictionary]()
你的 searchBarDelegate 会像
//MARK :- SearchBar 代表
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false
resignFirstResponder()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false
resignFirstResponder()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filteredItems = itemsArray.filter{
let stringa = [=10=]["name"] as! String!
let tmp: NSString = stringa
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
}
if(filteredItems.count == 0){
searchActive = false
} else {
searchActive = true
}
mTableView.reloadData()
}
主 tableView 委托将
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive == true{
return filteredItems.count
}
else{
return itemsArray.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("XXXXX") as! TableViewCellVC
let item:NSDictionary!
if searchActive == false {
item = itemsArray[indexPath.row]
}else{
item = filteredItems[indexPath.row]
}
return cell
}
首先不需要用两个表来查找和显示,这根本不是一个好办法。您还应该在搜索控制器上寻找苹果文档。您应该使用 Search Bar 和 Search Display Controller 并在其 viewDidLoad 方法中实现以下代码
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
以下方法根据 searchText 过滤您的主数组,并将结果放入 filteredData 数组。
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredData = array.filter { string in
return
string.lowercaseString.containsString(searchText.lowercaseString)
}
tableView.reloadData()
}
要允许 ViewController 响应搜索栏,它必须实施 UISearchResultsUpdating。打开 ViewController.swift 并在主要 ViewController class 之外添加以下 class 扩展:
extension MasterViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}
现在在您的 numberOfRowsInSection 和 cellForRowAtIndexPath 方法中执行以下操作
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return filteredData.count
}
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let string: String
if searchController.active && searchController.searchBar.text != "" {
string = filteredData[indexPath.row]
} else {
string = array[indexPath.row]
}
cell.textLabel?.text = string
return cell
}
更多详细信息,您可以访问以下网站。
https://www.raywenderlich.com/113772/uisearchcontroller-tutorial
我是 IOS 开发的新手,所以有些事情很难理解。
在我的应用程序中,当用户打开主屏幕时,我会显示一个任务列表和一个用于过滤任务的搜索。如果用户在我的搜索控制器中输入 "Monday",它应该只过滤星期一的任务。所有代码都可以正常工作,但我不知道这样做是否正确。
我有一个 Table 视图控制器,它具有所有任务并在用户打开应用程序时显示。因此,用户首先应该看到的是他的所有任务。
当用户在 table header 中打开搜索时,它应该过滤他的任务(从第一个 table 开始)并仅显示与过滤器对应的任务。它也工作正常。
现在,我的问题。我有两个 table。一个用于主视图控制器,另一个用于搜索控制器。 我必须为删除任务编写两次获取单元格和编辑样式的代码。
我做错了什么?我怎样才能对我的主 table 进行过滤器并将所有内容保留在主 table 中,而不是将所有内容复制到第二个控制器?
谢谢
取一个 Bool var searchActive = false
和一个空数组 var filteredItems = [NSDictionary]()
你的 searchBarDelegate 会像
//MARK :- SearchBar 代表
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false
resignFirstResponder()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false
resignFirstResponder()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filteredItems = itemsArray.filter{
let stringa = [=10=]["name"] as! String!
let tmp: NSString = stringa
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
}
if(filteredItems.count == 0){
searchActive = false
} else {
searchActive = true
}
mTableView.reloadData()
}
主 tableView 委托将
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive == true{
return filteredItems.count
}
else{
return itemsArray.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("XXXXX") as! TableViewCellVC
let item:NSDictionary!
if searchActive == false {
item = itemsArray[indexPath.row]
}else{
item = filteredItems[indexPath.row]
}
return cell
}
首先不需要用两个表来查找和显示,这根本不是一个好办法。您还应该在搜索控制器上寻找苹果文档。您应该使用 Search Bar 和 Search Display Controller 并在其 viewDidLoad 方法中实现以下代码
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
以下方法根据 searchText 过滤您的主数组,并将结果放入 filteredData 数组。
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredData = array.filter { string in
return
string.lowercaseString.containsString(searchText.lowercaseString)
}
tableView.reloadData()
}
要允许 ViewController 响应搜索栏,它必须实施 UISearchResultsUpdating。打开 ViewController.swift 并在主要 ViewController class 之外添加以下 class 扩展:
extension MasterViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}
现在在您的 numberOfRowsInSection 和 cellForRowAtIndexPath 方法中执行以下操作
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return filteredData.count
}
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let string: String
if searchController.active && searchController.searchBar.text != "" {
string = filteredData[indexPath.row]
} else {
string = array[indexPath.row]
}
cell.textLabel?.text = string
return cell
}
更多详细信息,您可以访问以下网站。 https://www.raywenderlich.com/113772/uisearchcontroller-tutorial