模糊搜索UISearchBar
#import "Search_ViewController.h"
@interface Search_ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate>
@property(nonatomic,strong)NSArray*dataArr;//数据源
@property(nonatomic,strong)NSArray*resultsArr;//搜索结果
@property(nonatomic,strong)UISearchBar*search;
@property(nonatomic,strong)UISearchDisplayController* searchPlay;
@property(nonatomic,strong)UITableView*aTableView;
@end
@implementation Search_ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"搜索";
_dataArr= [[NSArray alloc]initWithObjects:@"aaa",@"abc",@"aqq",@"bdc",@"gcd",@"mnb",@"zzz",nil];
[self createView];
// Do any additional setup after loading the view.
}
- (void) createView
{
_aTableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0,320,480)];
_aTableView.delegate=self;
_aTableView.dataSource=self;
[self.view addSubview:_aTableView];
}
#pragma mark -
#pragma mark UITableViewDelegate
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
_search= [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,40)];
_search.backgroundColor= [UIColor redColor];
_search.delegate=self;
_search.showsCancelButton=YES;
_search.keyboardType=UIKeyboardTypeDefault;
_searchPlay= [[UISearchDisplayController alloc]initWithSearchBar:self.search contentsController:self];
_searchPlay.delegate=self;
_searchPlay.searchResultsDataSource=self;
_searchPlay.searchResultsDelegate=self;
_searchPlay.active=NO;
return _searchPlay.searchBar;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar
{
NSLog(@"取消");
return YES;
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return tableView ==self.searchPlay.searchResultsTableView?0:40;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger row =0;
if([tableView isEqual:self.searchPlay.searchResultsTableView]) {
row = [self.resultsArr count];
}else{
row = [self.dataArr count];
}
return row;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([tableView isEqual:self.searchPlay.searchResultsTableView]) {
cell.textLabel.text= [self.resultsArr objectAtIndex:indexPath.row];
}else{
cell.textLabel.text= [self.dataArr objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark -
#pragma mark UISearchDisplayControllerDelegate
//text是输入的文本,scope是搜索范围
- (void) searchText:(NSString*)text andWithScope:(NSString*)scope
{
//CONTAINS是字符串比较操作符,
NSPredicate*result = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",text];
self.resultsArr= [self.dataArr filteredArrayUsingPredicate:result];
}
- (BOOL) searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
// searchString是输入的文本
//返回结果数据读取搜索范围在选择范围
NSArray*searScope = [self.searchPlay.searchBar scopeButtonTitles];//数组范围
[self searchText:searchString andWithScope:[searScope objectAtIndex:[self.searchPlay.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (BOOL) searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
NSString*inputText =self.searchPlay.searchBar.text;//搜索输入的文本
NSArray*searScope = [self.searchPlay.searchBar scopeButtonTitles];//索索范围
[self searchText:inputText andWithScope:[searScope objectAtIndex:searchOption]];
return YES;
}
模糊搜索UISearchBar的更多相关文章
- iOS 如何自定义UISearchBar 中textField的高度
iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...
- MySQL中进行模糊搜索的一些问题
在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符.SQL 通配符必须与 LIKE 运算符一起使用.在 SQL 中,可使用以下通配符:通配符 描述 % 替代一个或多个字符 ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- 更改UIsearchbar 的背景和cancel按钮(转)
修改背景 searchbar =[[UISearchBar alloc]initWithFrame:CGRectMake(,KTopBarHeight, , KTopBarHeight)]; sear ...
- UISearchBar控件-让我们来搞定!(转)
转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101dfg8.html 最近用到搜索功能.于是,经过不断的研究,终于,有点懂了. 那就来总结一下吧,好 ...
- 使用 JavaScript 实现简单候选项推荐功能(模糊搜索)【收藏】【转】
当我们使用 Google 等搜索功能时,会出现与搜索内容有关的候选项.使用 JavaScript 搜索字符串,通常会使用 indexOf 或者 search 函数,但是非常僵硬,只能搜索匹配特定词语. ...
- iOS开发——UI进阶篇(十九)UISearchBar控件简介
最近用到搜索功能.总结一下 搜索,无疑可以使用UISearchBar控件! 那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你 ...
- IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题
在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的: 这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需要让 ...
- 修改UISearchBar的背景颜色
当你看到这篇博客你就已经发现了用_searchBar.backgroundColor = [UIColor clearColor];来设置UISearchBar的颜色完全没有效果: 并且,有些方法是想 ...
随机推荐
- MONO 使用重要提示
重要提示:如果要使用mvc这样的框架(网址没有扩展名或扩展名没有正常规律),请一定用Jexus,而不要用apache/nginx等. ASP.NET跨平台初学者要注意: 1.不要开始就用freeBSD ...
- Qt 程序和窗口添加图标
Qt项目在打包发布之后都需要有个个性的程序图标和窗口图标,这样会使程序更加美观大方,下面我们分别来看如何给程序和窗口分别添加图标.我们需要两种格式的图片,一种是.ico的,用来给程序添加图标,一种是. ...
- shell if判断语句
测试脚本是否有语法错误: sh -n 脚本名 一.if语句: 二.逻辑运算解析: -f 判断文件是否存在 -d 判断目录是否存在 -eq 判断是否相等 -ne 判断是否不相等 -lt 小于 -g ...
- 针对binlog MIXED格式对表的增删改统计分析
通常情况下DB都是有非常完整的监控的,那么如果监控不完善,那么在某段时间内又发生了性能问题,那么我们也可以分析binlog来尝试找到问题.下面就贴出命令,其实就是常用的命令组合. mysqlbinlo ...
- 【液晶模块系列基础视频】5.3.X-GUI字体驱动3
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- OpenCV学习笔记——视频的边缘检测
使用Canny算子进行边缘检测,并分开输出到三个窗口中,再给每一个窗口添加文字 代码: #include"cv.h" #include"highgui.h" / ...
- JavaScript系列:replace()函数(第二个参数为函数)
语法 stringobject.replace(regexp/substr,replacement) 只讲解这个:参数replacement为函数时,这个参数函数的参数代表的意义,以及运行次数,及过程 ...
- 在HTML中如何把块的边框做成圆角
adius,就是半径的意思.用这个属性可以很容易做出圆角效果,当然,也可以做出圆形效果.原理很简单,“正方形的内切圆的半径等于正方形边长的一半”.下面就做一个红色的圆. 工具/原料 Adobe Dre ...
- 如何解包,编辑,重新打包boot images
HOWTO: Unpack, Edit, and Repack Boot Images http://forum.xda-developers.com/showthread.php?t=443994 ...
- 2016.09.14,英语,《Using English at Work》全书笔记
半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...