原文地址: http://ygamretuta.me/2011/08/10/ios-implementing-a-basic-search-uisearchdisplaycontroller-and-interface-builder/
今天,我会演示如何在iOS中实现本地搜索。代码是在苹果官方教程的基础上修改而来的。
新建Navigation-based Project。打开.xib文件,拖一个Search Bar and Search DisplayController 对象到Table View对象上方,如下图所示,选中File’s Owner ,打开Connections面板:
现在我们来创建Search Bar和SearchDisplay Controller的出口。打开Assistant Editor,按住ctrl键,将SearchDisplay Controller拖到ViewController 的头文件中。创建一个名为searchDisplayController的出口,然后点Connect。
同样的方法为Search Bar创建连接。现在ViewController的头文件看起来像这样:
|
#import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { UISearchDisplayController *searchDisplayController; UISearchDisplayController *searchBar; NSArray *allItems; NSArray *searchResults; } @property (nonatomic, retain) IBOutlet UISearchDisplayController *searchDisplayController; @property (nonatomic, retain) IBOutlet UISearchDisplayController *searchBar; @property (nonatomic, copy) NSArray *allItems; @property (nonatomic, copy) NSArray *searchResults; @end
|
你可能注意到,我初始化了两个NSArray。一个用于作为数据源,一个用于保存查找结果。在本文中,我使用字符串数组作为数据源。继续编辑.m文件前,别忘了synthesize相关属性:
|
@synthesize searchDisplayController; @synthesize searchBar; @synthesize allItems; @synthesize searchResults;
|
在viewDidLoad 方法中,我们构造了我们的字符串数组:
|
- (void)viewDidLoad { [super viewDidLoad]; // [self.tableView reloadData]; self.tableView.scrollEnabled = YES; NSArray *items = [[NSArray alloc] initWithObjects: @"Code Geass", @"Asura Cryin'", @"Voltes V", @"Mazinger Z", @"Daimos", nil]; self.allItems = items; [items release]; [self.tableView reloadData]; }
|
在Table View的返回TableView行数的方法中,我们先判断当前Table View是否是searchDisplayController的查找结果表格还是数据源本来的表格,然后返回对应的行数:
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger rows = 0; if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ rows = [self.searchResults count]; }else{ rows = [self.allItems count]; } return rows; }
|
在tableView:cellForRowAtIndexPath:方法里,我们需要做同样的事:
|
// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } /* Configure the cell. */ if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; }else{ cell.textLabel.text = [self.allItems objectAtIndex:indexPath.row]; } return cell; }
|
现在来实现当搜索文本改变时的回调函数。这个方法使用谓词进行比较,并讲匹配结果赋给searchResults数组:
|
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate]; }
|
接下来是UISearchDisplayController的委托方法,负责响应搜索事件:
|
#pragma mark - UISearchDisplayController delegate 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; }
|
运行工程,当你在搜索栏中点击及输入文本时,如下图所示:
分享到:
相关推荐
系统自带搜索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 进行混合使用