(1)SOLAddLocationViewController.h
#import <UIKit/UIKit.h> @interface SOLAddLocationViewController : UIViewController <UISearchDisplayDelegate, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> @end
(2)SOLAddLocationViewController.m
#import "SOLAddLocationViewController.h" #pragma mark - SOLAddLocationViewController Class Extension @interface SOLAddLocationViewController () // Results of a search @property (strong, nonatomic) NSMutableArray *searchResults; @property (strong, nonatomic) NSMutableArray *allCitys; @property (strong, nonatomic) NSArray *dataList; // Location search results display controller @property (strong, nonatomic) UISearchDisplayController *searchController; // -------- // Subviews // -------- // Location search bar @property (strong, nonatomic) UISearchBar *searchBar; // Navigation bar at the top of the view @property (strong, nonatomic) UINavigationBar *navigationBar; // Done button inside navigation bar @property (strong, nonatomic) UIBarButtonItem *doneButton; @property (strong, nonatomic) UITableView *mTableView; @end #pragma mark - SOLAddLocationViewController Implementation @implementation SOLAddLocationViewController - (void)viewDidLoad { self.view.backgroundColor = [UIColor clearColor]; self.view.opaque = NO; self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 480) style:UITableViewStyleGrouped]; self.mTableView.delegate = self; self.mTableView.dataSource = self; [self.mTableView setContentSize:CGSizeMake(320, 3000)]; [self.view addSubview:self.mTableView]; self.searchResults = [[NSMutableArray alloc]initWithCapacity:5]; self.navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)]; [self.view addSubview:self.navigationBar]; self.doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)]; // Inititalize and configure search bar self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 44)]; self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo; self.searchBar.placeholder = @"搜索城市"; self.searchBar.delegate = self; // Initialize and configure search controller self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self]; self.searchController.delegate = self; self.searchController.searchResultsDelegate = self; self.searchController.searchResultsDataSource = self; self.searchController.displaysSearchBarInNavigationBar = YES; self.searchController.navigationItem.rightBarButtonItems = @[self.doneButton]; self.navigationBar.items = @[self.searchController.navigationItem]; NSString *path = [[NSBundle mainBundle]pathForResource:@"Provinces" ofType:@"plist"]; self.dataList = [NSMutableArray arrayWithContentsOfFile:path]; self.allCitys = [NSMutableArray arrayWithCapacity:0]; } #pragma mark UIViewController Methods - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault animated:NO]; [self.searchController setActive:YES animated:NO]; [self.searchController.searchBar becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:NO]; [self.searchController setActive:NO animated:NO]; [self.searchController.searchBar resignFirstResponder]; } #pragma mark DoneButton Methods - (void)doneButtonPressed { } #pragma mark UISearchDisplayControllerDelegate Methods - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; return YES; } - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView { [tableView setFrame:CGRectMake(0, CGRectGetHeight(self.navigationBar.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.navigationBar.bounds))]; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.delegate = self; [self.view bringSubviewToFront:self.navigationBar]; } - (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope { NSMutableArray *searchResult = [[NSMutableArray alloc] initWithCapacity:0]; for(int i = 0; i < [self.allCitys count]; i++) { NSString *str = [self.allCitys objectAtIndex:i]; if([str rangeOfString:searchText].location != NSNotFound) [searchResult addObject:str]; } self.searchResults = [NSArray arrayWithArray:searchResult]; } #pragma mark UITableViewDelegate Methods - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"CellIdentifier"; // Dequeue cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // Configure cell for the search results table view if(tableView == self.searchController.searchResultsTableView) cell.textLabel.text = self.searchResults[indexPath.row]; else cell.textLabel.text = self.dataList[indexPath.section][@"Citys"][indexPath.row][@"C_Name"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(tableView == self.searchController.searchResultsTableView) { [tableView cellForRowAtIndexPath:indexPath].selected = NO; // code } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if(tableView == self.searchController.searchResultsTableView) return 1; else return [self.dataList count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(tableView == self.searchController.searchResultsTableView) return [self.searchResults count]; else { NSArray *cityArray = self.dataList[section][@"Citys"]; for(int i = 0; i < [cityArray count]; i++) { NSString *Name = cityArray[i][@"C_Name"]; if (![self.allCitys containsObject:Name]) [self.allCitys addObject:Name]; } return [cityArray count]; } } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *HeaderName; if(tableView == self.searchController.searchResultsTableView) HeaderName = @"搜索结果"; else HeaderName = self.dataList[section][@"p_Name"]; return HeaderName; } @end
(3)效果图
相关推荐
系统自带搜索UISearchDisplayController
TabBar+Table+UISearchDisplayController,页面跳转,搜索等应用;
使用UISearchDisplayController显示搜索结果博客对应的Demo。 原文地址:http://blog.csdn.net/jymn_chen/article/details/24608097
在UINavigationBar上显示UISearchBar, 并实现UISearchDisplayController功能的小Demo, 更多讲解在http://www.cnblogs.com/sely-ios/p/Sely.html
UISearchDisplayController搜索.rar
ios UISearchDisplayController 实现 UITableView 搜索功能 - LeslieFang - 博客园1
因为系统的UISearchDisplayController用起来各种坑,实在是累觉不爱,没办法只好自己写一个searchView,用法和UISearchDisplayController基本相同,代理方法可以自己添加,我就只写了一个。
IOS应用源码——UISearchDisplayController(搜索).zip
IOS应用源码——UISearchDisplayController(搜索).rar
IOS应用源码——TabBar+Table+UISearchDisplayController.rar
iOS8.0后UISearchController取代了UISearchDisplayController,这里对UISearchController的一些基本用法做了一个小Demo.
演示如何使用 NSFetchedResultsController 实现 UISearchDisplayController 来过滤核心数据对象 ##要求 Xcode 6 iOS 7.0+ ##例子 ##Credits CoreDataSearchBarDemo 由创建 ##License ######MIT 许可证 (MIT)...
UISearchController 内部自带 searchBar 等价于 UISearchDisplayController + UISearchBar
UISearchDisplayController适配 ##Description 本Demo的搜索栏UISearchBar脱离UITableView的tableHeaderView而存在,做了对iOS7,8的适配 ##效果图 ##测试环境 Xcode 6.4,模拟器iPhone5,5s,6,6 plus,真机5 iOS8.1
主要介绍了iOS应用中UISearchDisplayController搜索效果的用法,包括点击搜索出现黑条问题的解决方法,代码基于传统的Objective-C,需要的朋友可以参考下
原始示例使用 UISearchDisplayController,它在 iOS 8 中已弃用。 每个选项卡都使用 UISearchController 以不同的方式显示搜索结果: 在表格视图中,非常类似于使用 UISearchDisplayController 在集合视图中 ...
RoySearch//把UISearchBar和UISearchDisplayController封装在一起的一个Search控件,用时只需在需要调用的ViewController里调用即可。完美适配tableView。//A customed and packaged category that combined ...
通过 fmdb 进行的数据库的 基本操作(增删改查 )查找是使用 UISearchBar 和UISearchDisplayController 进行混合使用