編輯:關於Android編程
系統自帶的搜索頁面類 — UISearchDisplayController和UISearchController, 讓你更方便快捷的進行搜索功能開發. 效果如下:
NS_CLASS_DEPRECATED_IOS(3_0, 8_0, “UISearchDisplayController has been replaced with UISearchController”)
@interface SearchDisplayVC ()@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; }
- (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; }
//=============================================== #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]; }
//=============================================== #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例子中傳入的是一個單獨的控制器, 而UISearchDisplayController例子中傳入的是當前控制器中的一個tableview.
#import "SearchVC.h" #import "DCSearchResultTVC.h" @interface SearchVC ()@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 YES self.searchController.hidesNavigationBarDuringPresentation = YES; // default is YES self.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
但當將tableview的y值都為150時, 則UISearchDisplayController的效果還是跟上面一樣, 而UISearchController卻不一樣了, 因為他只是在tableview的布局上向上推了一個導航欄的高度. 如圖
self.definesPresentationContext = YES;一般他們都配合tableview來使用, 並把其searchbar作為tableview的tableviewheader. 這樣當你點擊searchbar時, 搜索頁面自動彈出. 你也可以手動present 搜索頁面, 需要執行present方法, 移除搜索頁面, 需要將其active設置為NO, 就可以了.
//preshent [self presentViewController:searchController animated:YES completion:nil]; //remove self.searchController.active = NO;
近幾個月傳出了谷歌正在加速整合Android和Chrome OS的消息,新操作系統定名Andromeda(仙女座)。外媒給出的最新進展是,目前已經有兩家型硬
這裡寫鏈接內容仿映客送小禮物的特效,順便復習一下屬性動畫,話不多說先看效果圖。需求分析可以看到整個動畫有幾部分組成,那我們就把每個部分拆分出來各個擊破。1.要顯示那些內容
ListView 簡介ListView 是安卓裡常用的控件, 本文介紹一下常用用法,以及優化等方法1、改寫activity_main.xml<LinearLayou
廣播作為android的四大組件之一,適用的地方還是很多,多用來特定條件情況下的通知。例如,開機,鬧鈴,電池電量過低等等。但還可以自定義廣播,用來兩個應用程序的通知。曾經