使用UISearchDisplayController
生活随笔
收集整理的這篇文章主要介紹了
使用UISearchDisplayController
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用UISearchDisplayController
雖然UISearchDisplayController名字中帶有controller,可他不是一個UIView相關的controller,因為,切換顯示UISearchDisplayController的時候,它并沒有在UIWindow中加載(UIWindow一次只能加載一個controller,所以可以證明它不屬于controller).
可以看看,他是繼承自NSObject,是多個控件組合在一起的一個東西.
以下是使用時的效果圖:
源碼:
// // RootViewController.m // TableViewSearch // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h"@interface RootViewController () <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>@property (nonatomic, strong) UITableView *dataView; // tableView @property (nonatomic, strong) UISearchDisplayController *searchController; // 搜索控制器 @property (nonatomic, strong) NSArray *names; // 數據源 @property (nonatomic, strong) NSArray *searchResults; // 搜索結果@end#define APP_HEIGHT [UIScreen mainScreen].applicationFrame.size.height #define SCR_HEIGHT [UIScreen mainScreen].bounds.size.height #define SCR_WIDTH [UIScreen mainScreen].bounds.size.width@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];// 初始化tableView_dataView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCR_WIDTH, SCR_HEIGHT - 20)style:UITableViewStylePlain];_dataView.delegate = self;_dataView.dataSource = self;[self.view addSubview:_dataView];// 初始化searchBarUISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];searchBar.delegate = self;searchBar.placeholder = @"搜索";[searchBar sizeToFit];// 將searchBar放置在tableView的HeaderView中self.dataView.tableHeaderView = searchBar;// 初始化search控制器并獲取searchBar以及當前視圖控制器_searchController = \[[UISearchDisplayController alloc] initWithSearchBar:searchBarcontentsController:self];_searchController.delegate = self;_searchController.searchResultsDataSource = self;_searchController.searchResultsDelegate = self;// tableView的數據源_names = @[@"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"]; }//=============================================== #pragma mark - #pragma mark - tableView相關代理 //=============================================== - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (tableView == self.dataView){return [self.names count];}else{return [self.searchResults count];} }- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[tableView deselectRowAtIndexPath:indexPathanimated:YES]; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *reusedStr = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr];if (cell == nil){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reusedStr];}if (tableView == self.dataView){cell.textLabel.text = self.names[indexPath.row];}else{cell.textLabel.text = self.searchResults[indexPath.row];}return cell; }//=============================================== #pragma mark - #pragma mark UISearchDisplayController相關代理 //===============================================- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {NSLog(@"將要開始搜索"); }- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {NSLog(@"開始搜索"); }- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {NSLog(@"將要結束搜索"); }- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {NSLog(@"結束搜索"); }- (void)searchDisplayController:(UISearchDisplayController *)controllerdidLoadSearchResultsTableView:(UITableView *)tableView {NSLog(@"加載了tableView"); }- (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView {NSLog(@"卸載了tableView"); }- (void)searchDisplayController:(UISearchDisplayController *)controllerwillShowSearchResultsTableView:(UITableView *)tableView {NSLog(@"將要顯示tableView"); }- (void)searchDisplayController:(UISearchDisplayController *)controllerdidShowSearchResultsTableView:(UITableView *)tableView {NSLog(@"已經顯示了tableView"); }- (void)searchDisplayController:(UISearchDisplayController *)controllerwillHideSearchResultsTableView:(UITableView *)tableView {NSLog(@"將要隱藏tableView"); }- (void)searchDisplayController:(UISearchDisplayController *)controllerdidHideSearchResultsTableView:(UITableView *)tableView {NSLog(@"已經隱藏了tableView"); }- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {NSLog(@"要重新加載tableView顯示搜索的數據么?");NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];self.searchResults = [self.names filteredArrayUsingPredicate:predicate];return YES; }- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {NSLog(@"要重新加載tableView的搜索域么?");return YES; }@endUISearchDisplayController自帶了tableView,也是走的代理方法,與你的controller中的tableView一樣的代理方法,這點需要注意:
?
?
轉載于:https://www.cnblogs.com/YouXianMing/p/3816660.html
總結
以上是生活随笔為你收集整理的使用UISearchDisplayController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软职位内部推荐-Senior SDE
- 下一篇: 累加求和 Accumulate.ja