日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UISearchControllerUISearchDisplayController

發布時間:2024/3/7 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UISearchControllerUISearchDisplayController 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

系統自帶的搜索頁面類 — UISearchDisplayController和UISearchController詳細的使用方法, 讓你更方便快捷的進行搜索功能開發. 效果如下:

UISearchDisplayController

NS_CLASS_DEPRECATED_IOS(3_0, 8_0, “UISearchDisplayController has been replaced with UISearchController”)

###1.先看下用到的屬性和代理

@interface SearchDisplayVC ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate, UISearchDisplayDelegate> @property (nonatomic, strong) UISearchDisplayController *searchDisplayController; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UITableView *resultTableView; @property (nonatomic, strong) NSArray *dataArr; @property (nonatomic, strong) NSArray *resultArr; @end - (NSArray *)createData {NSArray *dataArr = @[@"Aaliyah", @"Aaron", @"Abigail", @"Adam", @"Addison", @"Adrian", @"Aiden", @"Alex", @"Alexa", @"Alexander", @"Alexandra", @"Alexis", @"Allison", @"Alyssa", @"Amelia", @"Andrea", @"Andrew", @"Angel", @"Anna", @"Annabelle", @"Anthony", @"Aria", @"Ariana", @"Arianna", @"Ashley", @"Aubree", @"Aubrey", @"Audrey", @"Austin", @"Autumn", @"Ava", @"Avery", @"Ayden", @"Bailey", @"Bella", @"Benjamin", @"Bentley", @"Blake", @"Brandon", @"Brayden", @"Brianna", @"Brody", @"Brooklyn", @"Bryson", @"Caleb", @"Cameron", @"Camila", @"Carlos", @"Caroline", @"Carson", @"Carter", @"Charles", @"Charlotte", @"Chase", @"Chloe", @"Christian", @"Christopher", @"Claire", @"Colton", @"Connor", @"Cooper", @"Damian", @"Daniel", @"David", @"Dominic", @"Dylan", @"Easton", @"Eli", @"Elijah", @"Elizabeth", @"Ella", @"Ellie", @"Emily", @"Emma", @"Ethan", @"Eva", @"Evan", @"Evelyn", @"Faith", @"Gabriel", @"Gabriella", @"Gavin", @"Genesis", @"Gianna", @"Grace", @"Grayson", @"Hailey", @"Hannah", @"Harper", @"Henry", @"Hudson", @"Hunter", @"Ian", @"Isaac", @"Isabella", @"Isaiah", @"Jace", @"Jack", @"Jackson", @"Jacob", @"James", @"Jasmine", @"Jason", @"Jaxon", @"Jayden", @"Jeremiah", @"Jocelyn", @"John", @"Jonathan", @"Jordan", @"Jose", @"Joseph", @"Joshua", @"Josiah", @"Juan", @"Julia", @"Julian", @"Justin", @"Katherine", @"Kayden", @"Kayla", @"Kaylee", @"Kennedy", @"Kevin", @"Khloe", @"Kimberly", @"Kylie", @"Landon", @"Lauren", @"Layla", @"Leah", @"Levi", @"Liam", @"Lillian", @"Lily", @"Logan", @"London", @"Lucas", @"Lucy", @"Luis", @"Luke", @"Lydia", @"Mackenzie", @"Madeline", @"Madelyn", @"Madison", @"Makayla", @"Mason", @"Matthew", @"Maya", @"Melanie", @"Mia", @"Michael", @"Molly", @"Morgan", @"Naomi", @"Natalie", @"Nathan", @"Nathaniel", @"Nevaeh", @"Nicholas", @"Noah", @"Nolan", @"Oliver", @"Olivia", @"Owen", @"Parker", @"Peyton", @"Piper", @"Reagan", @"Riley", @"Robert", @"Ryan", @"Ryder", @"Samantha", @"Samuel", @"Sarah", @"Savannah", @"Scarlett", @"Sebastian", @"Serenity", @"Skylar", @"Sofia", @"Sophia", @"Sophie", @"Stella", @"Sydney", @"Taylor", @"Thomas", @"Trinity", @"Tristan", @"Tyler", @"Victoria", @"Violet", @"William", @"Wyatt", @"Xavier", @"Zachary", @"Zoe", @"Zoey"];return dataArr; }

###2. 創建當前頁面的tableview和 UISearchDisplayController

- (void)configureTableView {self.dataArr = [self create createData];self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 375, 600) style:UITableViewStylePlain];_tableView.delegate = self;_tableView.dataSource = self;[self.view addSubview:_tableView];[self configureTableView:_tableView]; }- (void)addSearchBarAndSearchDisplayController {UISearchBar *searchBar = [[UISearchBar alloc] init];[searchBar sizeToFit];searchBar.delegate = self;self.tableView.tableHeaderView = searchBar;self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];_searchDisplayController.delegate = self;_searchDisplayController.searchResultsDataSource = self;_searchDisplayController.searchResultsDelegate = self; }

###3. tableviewdatasource 和 tableviewdelegate

//=============================================== #pragma mark - #pragma mark UITableView //===============================================- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if ([tableView isEqual:_tableView]) {return self.dataArr.count;}return self.resultArr.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];if ([tableView isEqual:_tableView]) {cell.textLabel.text = _dataArr[indexPath.row];}else{cell.textLabel.text = _resultArr[indexPath.row];}return cell; }- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[tableView deselectRowAtIndexPath:indexPath animated:YES]; }

###4.UISearchDisplayDelegate

//=============================================== #pragma mark - #pragma mark UISearchDisplayDelegate //===============================================- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {NSLog(@" will begin search"); } - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {NSLog(@" did begin search"); } - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {NSLog(@" will end search"); } - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {NSLog(@" did end search"); } - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {NSLog(@" did load table");[self configureTableView:tableView]; } - (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView {NSLog(@" will unload table"); } - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {NSLog(@" will show table"); } - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {NSLog(@" did show table"); } - (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView {NSLog(@" will hide table"); } - (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {NSLog(@" did hide table"); } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {NSLog(@" should reload table for search string?");NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];self.resultArr = [self.dataArr filteredArrayUsingPredicate:predicate];return YES; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {NSLog(@" should reload table for search scope?");return YES; }


UISearchController

UISearchController例子中傳入的是一個單獨的控制器, 而UISearchDisplayController例子中傳入的是當前控制器中的一個tableview.

#import "SearchVC.h" #import "DCSearchResultTVC.h"@interface SearchVC ()<UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) UISearchController *searchController; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) DCSearchResultTVC *searchResultTVC; @property (nonatomic, strong) NSArray *dataArr;@end@implementation SearchVC- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];[self configureTableView];[self configureSearchController]; } - (NSArray *)createData {NSArray *dataArr = @[@"Aaliyah", @"Aaron", @"Abigail", @"Adam", @"Addison", @"Adrian", @"Aiden", @"Alex", @"Alexa", @"Alexander", @"Alexandra", @"Alexis", @"Allison", @"Alyssa", @"Amelia", @"Andrea", @"Andrew", @"Angel", @"Anna", @"Annabelle", @"Anthony", @"Aria", @"Ariana", @"Arianna", @"Ashley", @"Aubree", @"Aubrey", @"Audrey", @"Austin", @"Autumn", @"Ava", @"Avery", @"Ayden", @"Bailey", @"Bella", @"Benjamin", @"Bentley", @"Blake", @"Brandon", @"Brayden", @"Brianna", @"Brody", @"Brooklyn", @"Bryson", @"Caleb", @"Cameron", @"Camila", @"Carlos", @"Caroline", @"Carson", @"Carter", @"Charles", @"Charlotte", @"Chase", @"Chloe", @"Christian", @"Christopher", @"Claire", @"Colton", @"Connor", @"Cooper", @"Damian", @"Daniel", @"David", @"Dominic", @"Dylan", @"Easton", @"Eli", @"Elijah", @"Elizabeth", @"Ella", @"Ellie", @"Emily", @"Emma", @"Ethan", @"Eva", @"Evan", @"Evelyn", @"Faith", @"Gabriel", @"Gabriella", @"Gavin", @"Genesis", @"Gianna", @"Grace", @"Grayson", @"Hailey", @"Hannah", @"Harper", @"Henry", @"Hudson", @"Hunter", @"Ian", @"Isaac", @"Isabella", @"Isaiah", @"Jace", @"Jack", @"Jackson", @"Jacob", @"James", @"Jasmine", @"Jason", @"Jaxon", @"Jayden", @"Jeremiah", @"Jocelyn", @"John", @"Jonathan", @"Jordan", @"Jose", @"Joseph", @"Joshua", @"Josiah", @"Juan", @"Julia", @"Julian", @"Justin", @"Katherine", @"Kayden", @"Kayla", @"Kaylee", @"Kennedy", @"Kevin", @"Khloe", @"Kimberly", @"Kylie", @"Landon", @"Lauren", @"Layla", @"Leah", @"Levi", @"Liam", @"Lillian", @"Lily", @"Logan", @"London", @"Lucas", @"Lucy", @"Luis", @"Luke", @"Lydia", @"Mackenzie", @"Madeline", @"Madelyn", @"Madison", @"Makayla", @"Mason", @"Matthew", @"Maya", @"Melanie", @"Mia", @"Michael", @"Molly", @"Morgan", @"Naomi", @"Natalie", @"Nathan", @"Nathaniel", @"Nevaeh", @"Nicholas", @"Noah", @"Nolan", @"Oliver", @"Olivia", @"Owen", @"Parker", @"Peyton", @"Piper", @"Reagan", @"Riley", @"Robert", @"Ryan", @"Ryder", @"Samantha", @"Samuel", @"Sarah", @"Savannah", @"Scarlett", @"Sebastian", @"Serenity", @"Skylar", @"Sofia", @"Sophia", @"Sophie", @"Stella", @"Sydney", @"Taylor", @"Thomas", @"Trinity", @"Tristan", @"Tyler", @"Victoria", @"Violet", @"William", @"Wyatt", @"Xavier", @"Zachary", @"Zoe", @"Zoey"];return dataArr; } - (void)configureTableView {self.dataArr = [self createData];self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 375, 600) style:UITableViewStylePlain];_tableView.delegate = self;_tableView.dataSource = self;[self.view addSubview:_tableView]; } - (void)configureSearchController {self.searchResultTVC = [[DCSearchResultTVC alloc]initWithStyle:UITableViewStylePlain];self.searchResultTVC.resultsArray = self.dataArr;UINavigationController *resultVC = [[UINavigationController alloc] initWithRootViewController:_searchResultTVC];self.searchController = [[UISearchController alloc]initWithSearchResultsController:resultVC];self.searchController.searchResultsUpdater = self;self.searchController.delegate = self;self.searchController.dimsBackgroundDuringPresentation = YES; // default is YESself.searchController.hidesNavigationBarDuringPresentation = YES; // default is YESself.searchResultTVC.searchC = self.searchController;//解決SearchController偏移問題// self.definesPresentationContext = YES;/*** 1 ***///一般情況下都是配合tableview來使用, 所以可以直接將searchbar 放在 headerview中.當searbar激活的時候, 自動彈出searchVC//也可以通過下面的showTheSearchVC方法進行present時機控制_tableView.tableHeaderView = _searchController.searchBar; } //control the time to present the searchcontroller - (void)showTheSearchVC {[self presentViewController:self.searchController animated:YES completion:nil]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 10; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [[UITableViewCell alloc]init];cell.textLabel.text = @"222222";return cell; } #pragma mark - UISearchBarDelegate (which you use ,which you choose!!) - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar // return NO to not become first responder {return YES; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar // called when text starts editing { // [self showTheSearchVC]; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar // return NO to not resign first responder {return YES; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar // called when text ends editing { } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText // called when text changes (including clear) { } - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0) // called before text changes {return YES; }- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar // called when keyboard search button pressed { } - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar // called when bookmark button pressed { } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar // called when cancel button pressed { } - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) // called when search results button pressed { } - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0) { }#pragma mark - UISearchControllerDelegate (which you use ,which you choose!!) // These methods are called when automatic presentation or dismissal occurs. They will not be called if you present or dismiss the search controller yourself. - (void)willPresentSearchController:(UISearchController *)searchController{// do something before the search controller is presented } - (void)didPresentSearchController:(UISearchController *)searchController{ } - (void)willDismissSearchController:(UISearchController *)searchController{ } - (void)didDismissSearchController:(UISearchController *)searchController{ }// 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. - (void)presentSearchController:(UISearchController *)searchController{}#pragma mark - UISearchResultsUpdating (which you use ,which you choose!!) // Called when the search bar's text or scope has changed or when the search bar becomes first responder. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {//過濾的方法NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchController.searchBar.text];self.searchResultTVC.resultsArray = [self.dataArr filteredArrayUsingPredicate:predicate];//刷新數據[_searchResultTVC.tableView reloadData]; }@end


區別和注意

1.UISearchDisplayController是iOS3.0_iOS8.0, UISearchController 是iOS8.0及以后的.
2.他們兩者的搜索效果會由于tableview的布局不同而改變. UISearchController 會根據tableview的高度, 當present出UISearchController時, 會整體向上推64的高度. 而UISearchDisplayController不會跟tableview的布局有任何關系, 它都是present出一個正常置頂的UISearchDisplayController.
舉個例子, 一般的tableview的x值和y值都為0, 這樣的效果就是很正常的, searchbar置頂到導航欄上的. UISearchController和UISearchDisplayController的效果顯示一致, 如圖:

但當將tableview的y值都為150時, 則UISearchDisplayController的效果還是跟上面一樣, 而UISearchController卻不一樣了, 因為他只是在tableview的布局上向上推了一個導航欄的高度. 如圖

3.UISearchController在使用時可能會存在一個偏移量的問題, 搜索結果可能會有一塊留白. 如下圖.

這就需要在當前控制器加入一行代碼.

self.definesPresentationContext = YES;

4.一般他們都配合tableview來使用, 并把其searchbar作為tableview的tableviewheader. 或者自定義的searchBar, 只要你設置delete為當前控制器后, 當你點擊searchbar時, 搜索頁面自動彈出. 你也可以手動present 搜索頁面, 需要執行present方法, 移除搜索頁面, 需要將其active設置為NO, 就可以了.

//preshent [self presentViewController:searchController animated:YES completion:nil]; //remove self.searchController.active = NO;

總結

以上是生活随笔為你收集整理的UISearchControllerUISearchDisplayController的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久久久91 | 日韩天天操 | 日韩视频在线播放 | 日本大乳奶做爰 | 黄色av一区二区三区 | 美女脱光衣服让男人捅 | av在线短片 | 欧洲精品一区二区 | 免费在线观看a视频 | 一区二区三区波多野结衣 | 久久爱综合 | 黄在线视频| 欧美一区二区三区婷婷 | 免费黄在线 | 99色综合网 | 人妻少妇精品无码专区二区 | 精品一区二区三区四 | 久久久亚洲天堂 | 精品久久久中文字幕 | 精精国产xxxx视频在线播放 | 国产精品一区二区av白丝下载 | 国产露出视频 | 亚洲毛片一区二区 | 福利视频99 | 日韩免费电影一区 | 日韩视频在线观看 | 干一夜综合 | 在线免费看污网站 | 亚洲三级久久 | 森泽佳奈在线播放 | 97国产精东麻豆人妻电影 | 国产成人综合av | 污片在线免费看 | 国内av在线| 国产精品久久伊人 | 污视频在线观看免费 | 国产av 一区二区三区 | 超碰超碰超碰超碰超碰 | wwwxx欧美| 国产又黄又粗又猛又爽视频 | 五月婷婷爱 | 久久久久久久久久久久97 | 欧美成人精品一区二区 | 亚洲情热 | 黄色免费网页 | 蜜臀久久99精品久久一区二区 | 日韩一区二区视频在线播放 | 国产精品美女 | 成人美女在线观看 | 天天看夜夜 | 中文在线不卡视频 | 日韩不卡一二三区 | 99热综合| 99热.com | 国产伦乱视频 | 欧美经典一区二区 | 男人舔女人下部高潮全视频 | 国产日韩激情 | 欧美日本韩国一区二区三区 | 国产美女精品久久久 | 成人啪啪网站 | 美梦视频大全在线观看高清 | 日本成人黄色 | 一区二区三区丝袜 | 国产av人人夜夜澡人人爽 | 瑟瑟在线观看 | 欧美性生交xxxxx久久久缅北 | 日本少妇一级 | 精品1卡二卡三卡四卡老狼 日韩三级网 | av日韩av| 奇米影视亚洲 | 强行挺进皇后紧窄湿润小说 | 亚洲中出 | 99er在线观看 | 另类小说色综合 | 黄色在线视频观看 | 亚洲AV第二区国产精品 | 亚洲国产亚洲 | 欧美一区二区三区视频 | 老头老太吃奶xb视频 | 视频在线中文字幕 | 欧美日韩在线观看一区二区 | 欧美性视频在线 | 国产成人自拍视频在线 | 午夜影院在线免费观看 | 91在线播| 色综合av在线 | 悠悠色影院 | 熟妇毛片 | 99热这里只有精品首页 | 国产精品久久久久国产a级 国产一区二区在线播放 | 国产高清在线免费观看 | 国产无套粉嫩白浆内谢 | 欧美日韩在线视频一区二区 | 国产超碰在线观看 | 任你躁av一区二区三区 | 日本xxxx高潮少妇 | 999成人网| 中国大陆高清aⅴ毛片 |