如何在 iOS8 中实现 UISearchController?
Posted
技术标签:
【中文标题】如何在 iOS8 中实现 UISearchController?【英文标题】:How to implement UISearchController in iOS8? 【发布时间】:2015-06-25 02:40:49 【问题描述】:我尝试在 ios8 中实现 UISearchController 但失败了。
问题是当我更改了文本和范围按钮时,会向我显示注释。
当我更新搜索栏或范围按钮时,似乎甚至没有调用 updateSearchResultsForSearchController 函数。
这是我的代码:
class SearchTestController: UITableViewController, UISearchResultsUpdating
struct Candy
let category : String
let name : String
var searchcontroller = UISearchController(searchResultsController: nil)
func updateSearchResultsForSearchController(searchController: UISearchController)
filteredcandy = candies.filter() (candy:Candy) -> Bool in
let scopetest = ( self.category[self.searchcontroller.searchBar.selectedScopeButtonIndex] == "All" ) || ( candy.category == self.category[self.searchcontroller.searchBar.selectedScopeButtonIndex] )
//let texttest = candy.name.rangeOfString(self.searchcontroller.searchBar.text)
//let result = scopetest && (texttest != nil)
return scopetest
println(filteredcandy.count)
self.tableView.reloadData()
var candies = [Candy]()
var filteredcandy = [Candy]()
var category = ["Chocolate","Hard","Other","All"]
override func viewDidLoad()
super.viewDidLoad()
// Sample Data for candyArray
self.candies = [Candy(category:"Chocolate", name:"chocolate Bar"),
Candy(category:"Chocolate", name:"chocolate Chip"),
Candy(category:"Chocolate", name:"dark chocolate"),
Candy(category:"Hard", name:"lollipop"),
Candy(category:"Hard", name:"candy cane"),
Candy(category:"Hard", name:"jaw breaker"),
Candy(category:"Other", name:"caramel"),
Candy(category:"Other", name:"sour chew"),
Candy(category:"Other", name:"gummi bear")]
// Reload the table
self.tableView.reloadData()
self.tableView.tableHeaderView = searchcontroller.searchBar
searchcontroller.searchBar.sizeToFit()
searchcontroller.searchBar.showsSearchResultsButton = true
self.definesPresentationContext = true
searchcontroller.searchBar.scopeButtonTitles = category
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if searchcontroller.active
return self.candies.count
else
return self.candies.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
//ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
var candy : Candy
// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
if searchcontroller.active
candy = filteredcandy[indexPath.row]
else
candy = candies[indexPath.row]
// Configure the cell
cell.textLabel!.text = candy.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
【问题讨论】:
您是否为搜索控制器设置了代理? 【参考方案1】:将以下行添加到viewDidLoad()
searchcontroller.searchResultsUpdater = self
searchcontroller.delegate = self
更新:
在viewDidLoad()
中添加以下行
searchcontroller.searchBar.delegate = self
然后更新searchBar(_:selectedScopeButtonIndexDidChange:)
中的搜索结果
【讨论】:
谢谢,确实解决了问题。但是我还有一个问题,我发现结果只有在更新搜索文本时才更新,但在更新范围按钮时没有更新,这与 Apple 的文档不一致。这是一个错误吗? 谢谢,Praveen,我知道这会起作用,只是觉得这样做有点尴尬和多余,updateSearchResultsForSearchController() 函数应该像文档中描述的那样起作用。跨度> 这可能是一个错误,要让它工作,请从selectedScopeButtonIndexDidChange
调用updateSearchResultsForSearchController
,如here 所示。我不得不在我的应用程序中使用相同的 hack,它使用范围栏和 UISearchController
以上是关于如何在 iOS8 中实现 UISearchController?的主要内容,如果未能解决你的问题,请参考以下文章