1 UISearchBar
????????UISearchBar是由兩個(gè)subView組成的,一個(gè)是UISearchBarBackGround,另一個(gè)是UITextField.
1.1 代碼示例
1. UISearchBar(效果如下:)
①創(chuàng)建UISearchBar對(duì)象
//初始化,定義frame
UISearchBar?*bar?=?[[UISearchBar?alloc]?initWithFrame: CGRectMake(0, 50,?self.view.frame.size.width, 80)];
?//添加到控制器的視圖上?
?[self.view?addSubview: bar];
②UISerachBar的屬性
????//autocapitalizationType:包含4種類(lèi)型撼唾,但是有時(shí)候鍵盤(pán)會(huì)屏蔽此屬.
????//1.autocapitalizationType————自動(dòng)對(duì)輸入文本對(duì)象進(jìn)行大小寫(xiě)設(shè)置.
????bar.autocapitalizationType?=?UITextAutocapitalizationTypeWords;
????//2.autocorrectionType————自動(dòng)對(duì)輸入文本對(duì)象進(jìn)行糾錯(cuò)?
????bar.autocorrectionType?=?UITextAutocorrectionTypeYes;
????//3.設(shè)置title
? ? bar.prompt?=?@"全部聯(lián)系人";
????//4.設(shè)置顏色?
? ? bar.tintColor??=?[UIColor?purpleColor];????//渲染顏色
? ? bar.barTintColor?=?[UIColor?orangeColor];????//搜索條顏色
? ? bar.backgroundColor?=??[UIColor?purpleColor];????//背景顏色,因?yàn)槊AЧ?transulent).
? ? //5.translucent————指定控件是否會(huì)有透視效果
? ? bar.translucent?=?YES;
? ? //6.scopeButtonTitles(范圍buttonTitle)
????bar.scopeButtonTitles?=?@[@"精確搜索",@"模糊搜索"];
????bar.selectedScopeButtonIndex?=?1;//通過(guò)下標(biāo)指定默認(rèn)選擇的那個(gè)選擇欄
? ? //7.控制搜索欄下部的選擇欄是否顯示出來(lái)(需設(shè)置為YES?才能使用scopebar)
????bar.showScopeBar?=?YES;
? ? //8.設(shè)置搜索欄右邊的按鈕
????bar.showsSearchResultsButton??=?YES;//向下的箭頭
????bar.showsCancelButton?=?YES;?//取消按鈕
????bar.showsBookmarkButton?=??YES;?//書(shū)簽按鈕
????//9.提示內(nèi)容
????bar.placeholder?=?@"搜索";
????//10.取消鍵盤(pán)操作
? ? [searchBar?resignFirstResponder];
? ? //11.設(shè)置代理
? ? //UISearchBar不執(zhí)行搜索行為伸蚯,必須使用delegate婆翔,當(dāng)輸入搜索文本饲鄙、點(diǎn)擊button按鈕后,代理的方法
????會(huì)完成搜索對(duì)應(yīng)的操作驶俊。
? ? //.控件的委托枣购,委托要遵從UISearchBarDelegate協(xié)議嬉探,默認(rèn)是nil
? ? bar.delegate?=?self;
③代理要試實(shí)現(xiàn)的協(xié)議方法
1).輸入編輯事件處理
–?searchBar:textDidChange:
–?searchBar:shouldChangeTextInRange:replacementText:
–?searchBarShouldBeginEditing:
–?searchBarTextDidBeginEditing:
–?searchBarShouldEndEditing:
–?searchBarTextDidEndEditing:
2).點(diǎn)擊按鈕事件處理
–?searchBarBookmarkButtonClicked:
–?searchBarCancelButtonClicked:
–?searchBarSearchButtonClicked:
–?searchBarResultsListButtonClicked:
3).點(diǎn)擊Scope按鈕事件處理
–?searchBar: selectedScopeButtonIndexDidChange:
1.2 常用技巧
1.2.1 修改UISearchBar的背景
1.2.1.1 修改UISearchBar的背景顏色
????????UISearchBar是由兩個(gè)subView組成的擦耀,一個(gè)是UISearchBarBackGround,另一個(gè)是UITextField.要IB中沒(méi)有直接操作背景的屬性。方法是直接將UISearchBarBackGround移去涩堤。
seachBar =[[UISearchBar?alloc]?init];
seachBar.backgroundColor = [UIColor?clearColor];
for?(UIView?*subview?in?seachBar.subviews)
{
????if?([subview?isKindOfClass: NSClassFromString(@"UISearchBarBackground")])
????{
????????[subview?removeFromSuperview];
????????break;
????}
}
第二種解決的方法:
[[[[[[self searchBar] subviews] objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
1.2.1.2 為UISearchBar添加背景圖片
UISearchBar *m_searchBar =?[[UISearchBar?alloc]?initWithFrame: CGRectMake(0,?44,?320,?41)];
m_searchBar.delegate?=?self;
m_searchBar.barStyle?=?UIBarStyleBlackTranslucent;
m_searchBar.autocorrectionType?=?UITextAutocorrectionTypeNo;
m_searchBar.autocapitalizationType?=?UITextAutocapitalizationTypeNone;
m_searchBar.placeholder=?_(@"Search");
m_searchBar.keyboardType?=??UIKeyboardTypeDefault;
//為UISearchBar添加背景圖片
UIView?*segment =?[m_searchBar.subviews?objectAtIndex:0];
UIImageView?*bgImage =?[[UIImageView?alloc]?initWithImage: [UIImage?imageNamed: @"Images/search_bar_bg.png"]];
[segment?addSubview:?bgImage];
//<---背景圖片
[self.view?addSubview: m_searchBar];
[m_searchBar?release];?
1.2.2 取消UISearchBar調(diào)用的鍵盤(pán)
[searchBar?resignFirstResponder];
1.2.3 添加UISearchBar的兩種方法:
1.2.3.1 普通視圖添加
UISearchBar?*mySearchBar=?[[UISearchBar?alloc]
initWithFrame:CGRectMake(0.0,?0.0,?self.view.bounds.size.width,?45)];
?mySearchBar.delegate?=?self;
?mySearchBar.showsCancelButton?=?NO;
?mySearchBar.barStyle = UIBarStyleDefault;
?mySearchBar.placeholder = @"Enter?Name?or?Categary";
mySearchBar.keyboardType = UIKeyboardTypeNamePhonePad;
[self.view?addSubview: mySearchBar];
[mySearchBar?release];
1.2.3.2 在tableview上添加
代碼
????? //add?Table
????????UITableView?*myBeaconsTableView =?[[UITableView?alloc]
initWithFrame:CGRectMake(0,?0,?self.view.bounds.size.width,?self.view.bounds.size.height-40)
?????? ??????????????????????????????????????style:UITableViewStylePlain];
????????myBeaconsTableView.backgroundColor =?[UIColor?whiteColor];
????????myBeaconsTableView.delegate = self;
????????myBeaconsTableView.dataSource = self;
????????[myBeaconsTableView?setRowHeight:40];
????????//?Add?search bar
????????searchBar =?[[UISearchBar?alloc]?initWithFrame: CGRectMake(0.0,?0.0,?self.view.bounds.size.width,?40)];
????????searchBar.placeholder = @"Enter?Name";
????????searchBar.delegate?=?self;
????????myBeaconsTableView.tableHeaderView?=?searchBar;
????????searchBar.autocorrectionType?=?UITextAutocorrectionTypeNo;
????????searchBar.autocapitalizationType?=?UITextAutocapitalizationTypeNone;
????????[searchBar?release];
????????[self.view?addSubview: myBeaconsTableView];
????????[myBeaconsTableView?release];??
2 UISearchDisplayController使用
2.1 代碼示例
2. UISearchDisplayController(注:iOS8以上已經(jīng)棄用)
結(jié)合UISearchBar實(shí)現(xiàn)效果如下,實(shí)現(xiàn)搜索功能.
????提示:檢測(cè)Xcode系統(tǒng)版本代碼如下:
[[[UIDevice?currentDevice]?systemVersion]?floatValue]?>=?8.0???YES:?NO;
①.創(chuàng)建對(duì)象
????//需要?jiǎng)?chuàng)建UISearchBar對(duì)象,這里將對(duì)象都定義成了屬性
????self.searchBar?=?[[UISearchBar?alloc]?initWithFrame:CGRectMake(0,?0,?320,?40)];
????//設(shè)置UISearchBar屬性,上面有UISearchBar詳細(xì)介紹.
????self.searchBar.placeholder?=?@"enter?province?name";
????self.searchBar.autocorrectionType?=?UITextAutocorrectionTypeNo;
???self.searchBar.autocapitalizationType?=?UITextAutocapitalizationTypeAllCharacters;
????self.searchBar.scopeButtonTitles?=?[NSArray?arrayWithObjects: @"All", @"A", @"B", @"C", @"D",?nil];
????self.searchBar.showsScopeBar?=?YES;
????self.searchBar.keyboardType?=?UIKeyboardTypeNamePhonePad;
????self.searchBar.showsBookmarkButton?=?YES;
????//將seachBar作為控制器的透視圖,視圖控制器,繼承UITableViewController
????self.tableView.tableHeaderView?=?_searchBar;
????//將UIsearchBar添加到UIdSearchDispalyController上?
????self.displayController?=?[[UISearchDisplayController?alloc] initWithSearchBar: _searchBar?contentsController:?self];?
????????注:searchBar————在searchdisplaycontroller初始化后眷蜓,searchbar是不可修改的,是readonly屬性的.
②配置UISearchDisplayController的屬性
????//active————是搜索界面可視化胎围,默認(rèn)為no吁系,可用setActive方法設(shè)置.
????self.displayController.active?=?YES;
????//?searchResultsDataSource————搜索結(jié)果的數(shù)據(jù)源,代理對(duì)象(UITableViewDataSource)
????self.displayController.searchResultsDataSource?=?self;
????//searchResultsDelegate————搜索結(jié)果的委托,服從協(xié)議UITableViewDelegate???
????self.displayController.searchResultsDelegate?=?self;
③實(shí)現(xiàn)
/*
searchDisplayController?自身有一個(gè)searchResultsTableView,所以在執(zhí)行操作的時(shí)候首先要判斷是否是搜索結(jié)果的tableView白魂,如果是顯示的就是搜索結(jié)果的數(shù)據(jù)汽纤,
??如果不是,是TableView自身的view福荸,則需要顯示原始數(shù)據(jù)蕴坪。
*/
??if?(tableView?==?self.tableView)?{
??????return?self.dataArray.count;
??}?else{
??????NSPredicate?*predicate?=?[NSPredicate?predicateWithFormat: @"self?contains [cd] %@",?self.searchBar.text];
??????self.arr??=??[[NSMutableArray?alloc]?initWithArray:[self.dataArray?filteredArrayUsingPredicate: predicate]];
??????return?self.arr.count;
??}?
④使用UISearchDisplayDelegate的委托方法進(jìn)行搜索操作:
1).搜索狀態(tài)改變事件處理方法:
–?searchDisplayControllerWillBeginSearch:
–?searchDisplayControllerDidBeginSearch:
–?searchDisplayControllerWillEndSearch:
–?searchDisplayControllerDidEndSearch:?
2).裝載和卸載tableview事件處理方法:
– searchDisplayController: didLoadSearchResultsTableView:
–?searchDisplayController: willUnloadSearchResultsTableView:
3).顯示和隱藏tableview事件處理方法:
–?searchDisplayController:willShowSearchResultsTableView:
–?searchDisplayController:didShowSearchResultsTableView:
–?searchDisplayController:willHideSearchResultsTableView:
–?searchDisplayController:didHideSearchResultsTableView:
4).搜索條件改變時(shí)響應(yīng)事件處理方法:
–?searchDisplayController:shouldReloadTableForSearchString:
2.2 開(kāi)發(fā)技巧
2.2.1 重復(fù)使用SearchVC時(shí)要注意Cell重用問(wèn)題,清空上一次的搜索結(jié)果cell
? ? ? ? 對(duì)于搜索結(jié)果表視圖敬锐,IOS并不會(huì)隨著SearchVC的退出而移除(應(yīng)該是處于提高cell重用角度考慮)背传,所以第二次進(jìn)入視圖時(shí),默認(rèn)其實(shí)還是顯示的上次搜素結(jié)果cell滞造,一定要注意清除狀態(tài)续室。
? ? ? ? 另外栋烤,要注意表視圖的cell重用問(wèn)題谒养,上下滑動(dòng)時(shí),下面的cell可能就是用的上面視圖的某一個(gè)cell明郭,里面數(shù)據(jù)都沒(méi)有清空的买窟,要記得全部更新。
- (BOOL) updateWithCityInfoObject: (HJCityInfoObject*) obj
{
??? BOOL isChoosed = obj.isChoosed;
??? self.textLabel.text = obj.cityName;
??? if (!isChoosed) {
? ? ? ? HJCityObject * cObj = [HJManagerInstance getCityObjectByName: obj.cityName];
??????? if(cObj) {
??????????? isChoosed =TRUE;
??????????? obj.isChoosed = TRUE;
??????? }
??? }
??? if(isChoosed) {
??????? if(!_choosedImageView)
??????? {
??????????? _choosedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"ChoosedIcon"]];
??????????? [_choosedImageView setFrame: CGRectMake(self.frame.size.width - HJ_TableCell_X_Offset, (self.frame.size.height - HJ_AddCityCollectionCell_IsChoosedIcon_Height) / 2, HJ_AddCityCollectionCell_IsChoosedIcon_Height, HJ_AddCityCollectionCell_IsChoosedIcon_Width)];
??????????? [self addSubview: _choosedImageView];
??????? }
??? }
??? else
??? {
??????? if (_choosedImageView) {
??????????? [_choosedImageView removeFromSuperview];
??????????? _choosedImageView = nil;
??????? }
??? }
??? return YES;
}
3 UISearchController
3.1 代碼示例
3.1.1 效果圖
3.UISearchController(iOS8新特性)
UISearchController實(shí)現(xiàn)和上述效果基本一致,適用于iOS8以上版本
實(shí)現(xiàn)如下圖搜索效果
3.1.2 1)新建數(shù)據(jù)源屬性
代碼如下:
????1)新建控制器,繼承與UITableViewController,在extension中定義屬性
//存儲(chǔ)原來(lái)的數(shù)據(jù)
@property?(nonatomic,?retain)?NSArray*dataArr;
//存儲(chǔ)檢索后的數(shù)據(jù)
@property?(nonatomic,?retain)?NSArray*arr;
3.1.3 2)加載數(shù)據(jù),懶加載
-?(NSArray*)dataArr?{
????if?(!_dataArr)?{
????????self.dataArr?=?[NSArray?arrayWithObjects:@"Allan", @"Abbbb", @"Acccc", @"Bccccc", @"Cddddffk", @"Cddkllll", @"Ekkflfl", @"Ekljljfg", @"Leslie", @"Mm", @"Meinv", @"Meihi", @"Catilin", @"Arron",?@"211",?@"232",?@"243",?@"264",?@"285",?@"106",?@"311", @"432",?@"543",?@"664",?@"785",?@"806",?nil?nil];
????}
????return?_dataArr;
}
//如果檢索后的數(shù)據(jù)為空,將原有數(shù)據(jù)賦值給檢索數(shù)據(jù)
-?(NSArray*) arr?{
????if(!_arr)?{
????????self.arr?=?self.dataArr;
????}
????return?_arr;
}
3.1.4 3)加載UISearchController對(duì)象
-?(void)viewDidLoad?{
????[super?viewDidLoad];
????//cell重用機(jī)制,調(diào)用系統(tǒng)的
????[self.tableView?registerClass: [UITableViewCell?class] forCellReuseIdentifier: @"lock"];
????//創(chuàng)建搜索條,將自身設(shè)置為展示結(jié)果的控制器???
????UISearchController?*searchVC?= [[UISearchController?alloc] initWithSearchResultsController: nil];
????//設(shè)置渲染顏色
????searchVC.searchBar.tintColor?=?[UIColor?orangeColor];
????//設(shè)置狀態(tài)條顏色??
? ? searchVC.searchBar.barTintColor?=?[UIColor?orangeColor];
? ? //設(shè)置開(kāi)始搜索時(shí)背景顯示與否(很重要)?
????searchVC.dimsBackgroundDuringPresentation?=?NO;
? ? ?//適應(yīng)整個(gè)屏幕
? ? [searchVC.searchBar?sizeToFit];
? ? ?//設(shè)置顯示搜索結(jié)果的控制器
? ? ?searchVC.searchResultsUpdater?=?self;?//協(xié)議(UISearchResultsUpdating)
????//將搜索控制器的搜索條設(shè)置為頁(yè)眉視圖
???self.tableView.tableHeaderView?=?searchVC.searchBar;
}
3.1.5 4)實(shí)現(xiàn)協(xié)議中的方法,必須實(shí)現(xiàn)
-?(void) updateSearchResultsForSearchController: (UISearchController*)searchController
{
?????//謂詞檢測(cè)
????NSPredicate?*predicate?=?[NSPredicate?predicateWithFormat: @"self?contains?[cd]?%@", searchController.searchBar.text];
????//將所有和搜索有關(guān)的內(nèi)容存儲(chǔ)到arr數(shù)組
????self.arr?=?[NSMutableArray?arrayWithArray: [self.dataArr?filteredArrayUsingPredicate: predicate]];
????//重新加載數(shù)據(jù)?????
????[self.tableView?reloadData];
}
3.1.6 5)設(shè)置UITabelViewController中的其它
#pragma?mark?-?Table?view?data?source
//設(shè)置有多少個(gè)分區(qū)
-?(NSInteger) numberOfSectionsInTableView: (UITableView*)tableView?{
????//?Return?the?number?of?sections.
????return?1;
}
//每個(gè)分區(qū)有多少行數(shù)據(jù)
-?(NSInteger) tableView: (UITableView?*)tableView?numberOfRowsInSection:
(NSInteger)section {
????return?self.arr.count;
}
-?(UITableViewCell?*) tableView: (UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath*)indexPath?{
????self.cell?=?[tableView?dequeueReusableCellWithIdentifier: @"lock" forIndexPath: indexPath];
????//設(shè)置cell上展示的內(nèi)容,即搜索后的數(shù)據(jù)
????self.cell.textLabel.text?=?self.arr[indexPath.row];
????return?self.cell;
}
? ? ? ? 注.以上是實(shí)現(xiàn)搜索框搜索空能.(當(dāng)搜索內(nèi)容為空時(shí),返回的時(shí)所有數(shù)據(jù),如果搜索內(nèi)容為空,返回空時(shí),需要進(jìn)行其它修改操作.)
4 開(kāi)發(fā)技巧
4.1 兼容IOS7薯定、8并結(jié)合自定義導(dǎo)航條使用
4.1.1 核心思路
? ? ? ? 雖然UISearchController中也有Search bar變量始绍,但是此界面中的Search bar必須自己新建定義,不能通過(guò)重用UISearchController. Search bar變量來(lái)實(shí)現(xiàn)话侄,否則會(huì)出現(xiàn)很多布局與交互異常問(wèn)題亏推。
? ? ? ? 此界面中的Search bar只用于觸發(fā)搜索操作,從而顯示UISearchController頁(yè)面年堆。
? ? ? ? 使用時(shí)注意:在searchBarTextDidBeginEditing事件中必須將當(dāng)前子視圖上移44px(即移到導(dǎo)航條中)吞杭,以便達(dá)到讓用戶(hù)覺(jué)得UISearchController界面中的Search bar就是此Search bar依上去的。
? ? ? ? 而用戶(hù)退出UISearchController界面時(shí)变丧,同樣要記得重新設(shè)置此界面的布局芽狗,以便讓用戶(hù)覺(jué)得此Search bar是UISearchController界面中下移下來(lái)的。
4.1.2 初始化
4.1.2.1 通用初始化
????float sysVer = [HJUtility getSystemVersion];
??? [self initSearchBarView];
??? if (sysVer <= 8.0) {
??????? [self initSearchDisplayVC];
??? }
??? else
??? {
??????? [self initSearchVC];
??? }
4.1.2.2 initSearchBar
- (void) initSearchBarView
{
??? if (!self.searchBar) {
??????? _searchResultArray = [[NSArray alloc] init];
???? ???self.searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(0, 64, self.view.frame.size.width, 44)];
??????? [self.searchBar setPlaceholder: @"搜索城市中文/拼音/字母"];
??????? self.searchBar.delegate = self;
??????? [self.view addSubview: _searchBar];
? ??}
}
4.1.2.3 initSearchDisplayVC
- (void) initSearchDisplayVC
{
??? _searchDisplayVC = [[UISearchDisplayController alloc] initWithSearchBar: _searchBar contentsController: self];
??? _searchDisplayVC.delegate = self;
??? _searchDisplayVC.searchResultsDataSource = self;
??? _searchDisplayVC.searchResultsDelegate = self;
??? _searchDisplayVC.active = NO;
??? //注冊(cè)
??? [_searchDisplayVC.searchResultsTableView registerClass: [HJCitySearchResultTableViewCell class] forCellReuseIdentifier: @"HJCitySearchResultTableViewCell"];
//??? [_searchDisplayVC setDisplaysSearchBarInNavigationBar: NO];
}
4.1.2.4 initSearchVC
- (void) initSearchVC
{
??? UIViewController * vc = [[UIViewController alloc] init];
??? [vc.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
??? _resultTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style: UITableViewStylePlain];?
??? _resultTableView.dataSource = self;
??? _resultTableView.delegate = self;
??? //注冊(cè)
??? [_resultTableView registerClass: [HJCitySearchResultTableViewCell class] forCellReuseIdentifier: @"HJCitySearchResultTableViewCell"];
??? [vc.view addSubview: _resultTableView];
??? [_searchVC.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
??? _searchVC = [[UISearchController alloc] initWithSearchResultsController: vc];
??? _searchVC.delegate = self;
??? //設(shè)置開(kāi)始搜索時(shí)背景顯示與否(很重要)
??? _searchVC.dimsBackgroundDuringPresentation = YES;
??? [_searchVC setHidesNavigationBarDuringPresentation: YES];
??? //適應(yīng)整個(gè)屏幕
??? [_searchVC.searchBar sizeToFit];
??? //設(shè)置顯示搜索結(jié)果的控制器
??? _searchVC.searchResultsUpdater = self; //協(xié)議(UISearchResultsUpdating)
??? [_searchVC.searchBar setPlaceholder: @"搜索城市中文/拼音/字母"];
??? [_searchVC.searchBar setDelegate: self];
}
4.1.3 委托處理
4.1.3.1 UISearchBarDelegate委托處理
#pragma mark - UISearchBarDelegate 委托處理
- (void) searchBarTextDidBeginEditing: (UISearchBar*)searchBar
{
? ? [self addResignFirstResponderGestureTap];
??? if (searchBar == _searchBar) {
??????? if (_searchVC) {
??????????? [self.view addSubview: _searchVC.view];
??????????? [_searchVC setActive: YES];
??????????? [self layouSubViewsWhenSearchVCDisplay];
??????? }
??? }
}
- (void)searchBar: (UISearchBar *)searchBar textDidChange: (NSString*)searchText
{
??? _searchResultArray = [HJDatabaseManagerInstance selectCityInfoObjectArrayFromTableWithQueryText: searchBar.text];
??? if (_searchDisplayVC) {
??????? [_searchDisplayVC.searchResultsTableView reloadData];
??? }
??? else if (_searchVC){
??????? [_resultTableView reloadData];
??? }
}
- (void) searchBarCancelButtonClicked: (UISearchBar*)searchBar
{
??? [self resetLayoutSubViews];
}
4.1.3.2 UISearchDisplayControllerDelegate
- (BOOL) searchDisplayController: (UISearchDisplayController *)controller
shouldReloadTableForSearchString: (NSString*)searchString{
??? // Return YES to cause the search result table view to be reloaded.
??? return YES;
}
- (BOOL) searchDisplayController: (UISearchDisplayController *)controller
shouldReloadTableForSearchScope: (NSInteger)searchOption{
??? // Return YES to cause the search result table view to be reloaded.
??? return YES;
}
- (void) searchDisplayControllerWillBeginSearch: (UISearchDisplayController*)controller
{
??? [self layouSubViewsWhenSearchVCDisplay];
}
- (void) searchDisplayControllerDidEndSearch: (UISearchDisplayController*)controller{
??? [self resetLayoutSubViews];
??? [_searchBar resignFirstResponder];
}
4.1.3.3 UISearchResultsUpdating委托
// Called when the search bar's text or scope has changed or when the search bar becomes first responder.
- (void) updateSearchResultsForSearchController: (UISearchController*)searchController
{
??? //已經(jīng)在Searchbar委托方法中處理了
}
4.1.3.4 UISearchControllerDelegate
- (void) willPresentSearchController: (UISearchController*)searchController
{
}
- (void) willDismissSearchController: (UISearchController*)searchController
{
}
- (void) didDismissSearchController: (UISearchController*)searchController
{
??? if (_searchVC) {
????????[_searchVC setSearchResultsUpdater: nil];
? ? ? ? if ( ![self.view.subviews containsObject: _searchBar]) {
? ? ? ? ? ? [self.view addSubview: _searchBar];
? ? ? ? ? ? [self.view bringSubviewToFront: _searchBar];
? ? ? ? }
? ? ? ? [_searchVC setActive: NO];
? ? ? ? [_searchBar setDelegate: self];
? ? ? ? [_searchBar setPlaceholder: @"搜索城市中文/拼音/字母"];
??? }
??? [self resetLayoutSubViews];
}
4.1.4 退出
- (void) closeView: (id)sender
{
??? if (_searchVC) {
??????? [_searchVC.searchBar setDelegate: nil];
??????? [_searchVC setDelegate: nil];
??????? [_searchVC setSearchResultsUpdater: nil];
??????? [_searchVC setActive: NO];
??????? [_searchVC.view removeFromSuperview];
??? }
??? else if(_searchDisplayVC){
??? }
??? [_searchBar setDelegate: nil];
??? [self.navigationController.view removeFromSuperview];
??? [self.navigationController removeFromParentViewController];
}
4.1.5 輸入欄外點(diǎn)擊手勢(shì)處理
- (void) addResignFirstResponderGestureTap
{
??? if (!_resignFirstResponderGesture) {
??????? _resignFirstResponderGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(resignFirstResponderGestureTap)];
??? }
??? if (_searchDisplayVC) {
??????? if (![_searchDisplayVC.searchResultsTableView.gestureRecognizers containsObject: _resignFirstResponderGesture]) {
??????????? [_searchDisplayVC.searchResultsTableView addGestureRecognizer: _resignFirstResponderGesture];
??????? }
??? }
??? else if (_searchVC)
??? {
??????? if (![_resultTableView.gestureRecognizers containsObject: _resignFirstResponderGesture]) {
??????????? [_resultTableView addGestureRecognizer: _resignFirstResponderGesture];
??????? }
??? }
}
- (void) resignFirstResponderGestureTap
{
??? if (_searchDisplayVC) {
??????? if ([_searchBar isFirstResponder]) {
??????????? [_searchBar resignFirstResponder];
??????????? [_searchDisplayVC.searchResultsTableView becomeFirstResponder];
??????????? [_searchDisplayVC.searchResultsTableView removeGestureRecognizer: _resignFirstResponderGesture];
??????? }
??? }
??? else if(_searchVC)
??? {
??????? if ([_searchVC.searchBar isFirstResponder]) {
??????????? [_searchVC.searchBar resignFirstResponder];
??????????? [_resultTableView becomeFirstResponder];
??????????? [_resultTableView removeGestureRecognizer: _resignFirstResponderGesture];
??????? }
??? }
}
5 參考鏈接
UISearchBar和UISearchDisplayController的使用
http://www.cnblogs.com/langtianya/p/4114532.html
UISearchBar和UISearchDisplayController
http://www.cnblogs.com/VincentXue/archive/2012/08/30/2664119.html
SearchBar+SearchDisplayController實(shí)現(xiàn)實(shí)時(shí)搜索??
http://www.cocoachina.com/bbs/read.php?tid=131433
ios UISearchDisplayController實(shí)現(xiàn)UITableView搜索功能
http://www.cnblogs.com/lesliefang/p/3929677.html
【學(xué)習(xí)ios之路:UI系列】(UISearchBar,UISearchDisplayController)和UISearchController(iOS8新特性)
http://blog.csdn.net/zfx5130/article/details/43371403
UISearchController基礎(chǔ)使用
http://www.cnblogs.com/xiaojywuxy/p/4247963.html
Sample code to demonstrate using UISearchController In acollection view
https://github.com/dempseyatgithub/Sample-UISearchController
Insert a UISearchBar in IOS 8, Xcode 6
http://stackoverflow.com/questions/27632093/insert-a-uisearchbar-in-ios-8-xcode-6/27649289#27649289