// // FirstViewController.m // UISearchBarControllerText // // Created by 杜承玖 on 14/10/24. // Copyright (c) 2014年 com.redianying. All rights reserved. // #import "FirstViewController.h" @interface FirstViewController () <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UISearchBar * sb; @property (nonatomic, strong) UITableView * tv; @property (nonatomic, strong) UISearchDisplayController * sdc; @property (nonatomic, copy) NSArray *allItems; @property (nonatomic, copy) NSArray *searchResults; @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *items = @[@"Code Geass", @"Asura Cryin'", @"Voltes V", @"Mazinger Z", @"Daimos"]; self.allItems = items; _sb = [[UISearchBar alloc] init]; _sb.placeholder = @"输入名称"; _sb.barStyle = UIBarStyleDefault; _sb.delegate = self; [_sb setScopeButtonTitles:@[@"First", @"Last"]]; [_sb setAutocapitalizationType:UITextAutocapitalizationTypeNone]; _tv = [[UITableView alloc] initWithFrame:self.view.frame]; _tv.frame = CGRectMake(0, 22, 320, 500); _tv.delegate = self; _tv.dataSource = self; [self.view addSubview:_tv]; [_tv setTableHeaderView:_sb]; _sdc = [[UISearchDisplayController alloc] initWithSearchBar:_sb contentsController:self]; _sdc.delegate = self; [_sdc setSearchResultsDataSource:self]; [_sdc setSearchResultsDelegate:self]; } #pragma mark - SearchBar - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ // _tv.frame = CGRectMake(0, 22, 320, 500); return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ // _tv.frame = CGRectMake(0, 22, 320, 500); return YES; } #pragma mark - SearchDisplayController - (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; } #pragma mark - Function - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate]; } #pragma mark - UITableView - (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; } - (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]; 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; } @end
相关推荐
UISearchBar使用Demo,UISearchBar使用Demo,UISearchBar使用Demo
uisearchbar 使用指南
在UINavigationBar上显示UISearchBar, 并实现UISearchDisplayController功能的小Demo, 更多讲解在http://www.cnblogs.com/sely-ios/p/Sely.html
通过UItextField自定义UISearchBar 描述了UItextField代理的来实现uisearchbar功能
UISearchBar的一个基本重新实现更容易的主题更换和更好的动画
iOS搜索。 iOS-UISearchBar
UISearchBar 修改占位符的放大镜,修改cancal,修改占位符。
UISearchBar扩展类,可以实现点击完成收回键盘,只需导入项目,在需要的时候引入头文件即可,使用非常简单
iOS开发套件中自带的UISearchBar搜索框我们平时经常可以用到,我们可以在默认的基础上修改文字颜色、背景颜色和背景图片等,这里我们稍微总结一下iOS中的UISearchBar搜索框组件基础使用指南.
UISearchBar
search bar
UISearchBar 不同样式的搜索框,代码简单
理论:http://blog.csdn.net/z251257144/article/details/7580307
SSSearchBar, 一个干净,易于使用,完美的替代 UISearchBar #SSSearchBar一个干净,易于使用,完美的替代 UISearchBar 。 ( 仅限 iOS 7 )要使用的##How下载代码并将 sssearchbar/* 下的文件添加到你的项目中。将...
主要介绍了IOS 开发之UISearchBar 详解及实例的相关资料,主要介绍 IOS UISearchBar的使用,附有实例代码,需要的朋友可以参考下
主要介绍了iOS中使用UISearchBar控件限制输入字数的实现方法的相关资料,需要的朋友可以参考下
实现UISearchBar的搜索功能,对于模糊搜索做的小demo.
UISearchBarTest. UISearchBar搜索栏示例程序v