前言
在楊武老師講的TableView視頻里面谷朝,有這么一段介紹了搜索的部分飒硅。
UISearchBar(iOS2+)
UISearchDisplayController(iOS7)
UISearchController(iOS8)
UISearchController是作為現(xiàn)在最新的搜索工具涎嚼,具體怎么用也沒(méi)有講解奏夫,正好我也沒(méi)有學(xué)過(guò)刮吧,所以想從文檔入手移必,查看怎么去使用一個(gè)UISearchController。
全文語(yǔ)言為Swift遭殉。
全部資料來(lái)自Document and Api Reference舱禽。
初始化
![init][1]
這里的參數(shù)searchResultsController為展示搜索結(jié)果的UIViewController類(lèi)的對(duì)象,如果展示搜索結(jié)果的界面與自己搜索結(jié)果的頁(yè)面是同一個(gè)視圖恩沽,那么就直接填寫(xiě)nil。
現(xiàn)在就來(lái)初始化一個(gè)UISearchController翔始。并將其放入自己的TableView中罗心,因?yàn)楸酒饕v解UISearchController,所以創(chuàng)建TableView的過(guò)程略過(guò)。
var searchController:UISearchController = UISearchController.init(searchResultsController: nil)
//MARK:- Life cycle
override func viewDidLoad()
{
super.viewDidLoad()
self.tableview.tableHeaderView = self.searchController.searchBar;
}
Active
![Active][2]
active屬性表示搜索界面的狀態(tài)城瞎,只讀屬性渤闷。
Delegate
![delegate][3]
delegate就是UISearchController的代理。
如果要設(shè)置代理脖镀,首先要繼承UISearchControllerDelegate這個(gè)協(xié)議飒箭。
在點(diǎn)進(jìn)去協(xié)議后可以看到里面的方法。
public protocol UISearchControllerDelegate : NSObjectProtocol {
// These methods are called when automatic presentation or dismissal occurs.
//They will not be called if you present or dismiss the search controller yourself.
@available(iOS 8.0, *)
optional public func willPresentSearchController(searchController: UISearchController)
@available(iOS 8.0, *)
optional public func didPresentSearchController(searchController: UISearchController)
@available(iOS 8.0, *)
optional public func willDismissSearchController(searchController: UISearchController)
@available(iOS 8.0, *)
optional public func didDismissSearchController(searchController: UISearchController)
// Called after the search controller's search bar has agreed to begin editing or when 'active' is set to YES.
//If you choose not to present the controller yourself or do not implement this method, a default presentation is performed on your behalf.
@available(iOS 8.0, *)
optional public func presentSearchController(searchController: UISearchController)
}
可以看出都是可選方法蜒灰,應(yīng)該是UISearchController的Life cycle弦蹂。我們可以結(jié)合剛剛的active屬性,去模擬這些方法發(fā)生的時(shí)間順序强窖。
//MARK:- UISearchControllerDelegate
func presentSearchController(searchController: UISearchController) {
print("presentSearchController \(searchController.active)");
}
func willPresentSearchController(searchController: UISearchController) {
print("willPresentSearchController \(searchController.active)");
}
func didPresentSearchController(searchController: UISearchController) {
print("didPresentSearchController \(searchController.active)");
}
func didDismissSearchController(searchController: UISearchController) {
print("didPresentSearchController \(searchController.active)");
}
func willDismissSearchController(searchController: UISearchController) {
print("willDismissSearchController \(searchController.active)");
}
運(yùn)行程序凸椿。
點(diǎn)擊搜索欄。
![s1][4]
presentSearchController false
willPresentSearchController false
didPresentSearchController true
![s2][5]
點(diǎn)擊cancel
willDismissSearchController true
didPresentSearchController false
dimsBackgroundDuringPresentation
![4.png-120kB][6]
決定在搜索時(shí)翅溺,底層的內(nèi)容是否要變暗脑漫。
默認(rèn)值是true髓抑。
默認(rèn)情況就是這樣:
![dimTure][7]
如果我們?cè)O(shè)定為false
設(shè)定代碼
self.searchController.dimsBackgroundDuringPresentation = false
那么現(xiàn)在搜索時(shí)界面就是這樣:
![dimFalse][8]
hidesNavigationBarDuringPresentation
![hideNav][9]
在搜索欄使用的時(shí)候是否需要隱藏NavigationBar,默認(rèn)值為true优幸。
效果圖:
![hideTure][10]
如果我們?cè)O(shè)定為false
設(shè)定代碼
self.searchController.hidesNavigationBarDuringPresentation = false
那么現(xiàn)在界面就是這樣:
![hideFalse][11]
searchBar
![searchBar][12]
search controller 所使用的 search bar 對(duì)象吨拍,只讀屬性。
searchResultsController
![searchController][13]
管理搜索結(jié)果的 view controller网杆,只讀屬性羹饰。
searchResultsUpdater
![searchResultsUpdater][14]
該對(duì)象更新搜索結(jié)果的view controller的內(nèi)容。
因?yàn)閕d< UISearchResultsUpdating >可以看出這也是一個(gè)delegate
所以這時(shí)候我們給class繼承UISearchResultsUpdating協(xié)議跛璧。
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource ,UISearchControllerDelegate ,UISearchResultsUpdating
并設(shè)置代理严里。
self.searchController.searchResultsUpdater=self
點(diǎn)進(jìn)UISearchResultsUpdating協(xié)議中,查看其代理方法追城。
![update][15]
發(fā)現(xiàn)只有一個(gè)方法刹碾,- updateSearchResultsForSearchController:
,該方法在點(diǎn)擊search bar或者用戶改變search bar的時(shí)候會(huì)被調(diào)用。
另外該方法是Required座柱,說(shuō)明必須要實(shí)現(xiàn)迷帜。
//MARK:- UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController)
{
}
結(jié)尾
updateSearchResultsForSearchController這個(gè)方法是UISearchController的關(guān)鍵部分,更新將稍后放出色洞。
[1]: http://static.zybuluo.com/zandhappy/49xgvujsu7f8hl03iy02eba5/1.png
[2]: http://static.zybuluo.com/zandhappy/26wknsmzx4w8rwlimonuq0uo/2.png
[3]: http://static.zybuluo.com/zandhappy/uusxt8eodiblqfj8o6wvu8zp/3.png
[4]: http://static.zybuluo.com/zandhappy/qinsgrldr0qa1a1m3eldgg9z/s1.png
[5]: http://static.zybuluo.com/zandhappy/424585967r3kk0afik0o5pfq/s2.png
[6]: http://static.zybuluo.com/zandhappy/nm6bu5uc6xx85qluh2w0eq3q/4.png
[7]: http://static.zybuluo.com/zandhappy/x1icvr4xoelekvg80mm8u7ov/dimTure.png
[8]: http://static.zybuluo.com/zandhappy/olt64voim51c6oirfidzjqgy/dimno.png
[9]: http://static.zybuluo.com/zandhappy/076mjoufk3ecky99jdblzlms/hideNav.png
[10]: http://static.zybuluo.com/zandhappy/ohzqydions6b5nbi1dvtmzr8/hideTure.png
[11]: http://static.zybuluo.com/zandhappy/ef7klmo7uf4eby83ka2bu2sb/hideFalse.png
[12]: http://static.zybuluo.com/zandhappy/hjuea4g5b51i81q3tf0bsy3h/searchBar.png
[13]: http://static.zybuluo.com/zandhappy/w22iiq9e0t97q5at5l65rfq8/searchController.png
[14]: http://static.zybuluo.com/zandhappy/ci63l8wyva1wq4irbk0wxhav/searchResultsUpdater%20.png
[15]: http://static.zybuluo.com/zandhappy/f4vgv609mb81aaq1ac1rucc6/update.png