1. 简单的搜索
![](https://img-blog.csdn.net/20161026155144644?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20161026155045534?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#import "ViewController.h"
#import "UIViewExt.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField * rearchTextFiled;
@property (nonatomic,strong)UIView *inputView;
@property (nonatomic,strong)UIImageView *imagSearchView;
@property (nonatomic,strong)UIView *bgView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfInitRearchTextfiled];
[selfInitKeyBordNotification];
[selfhiddenSearchAnimation];
}
-(void)InitKeyBordNotification
{
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(showKeyBoardAction:)name:UIKeyboardWillShowNotificationobject:self.view.window];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(hideKeyBoardAction:)name:UIKeyboardWillHideNotificationobject:self.view.window];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(changKeyBoardAction:)name:UIKeyboardDidChangeFrameNotificationobject:self.view.window];
}
-(void)showKeyBoardAction:(NSNotification *)noti
{
[UIViewanimateWithDuration:2animations:^{
[selfSearchANimation];
}];
}
-(void)hideKeyBoardAction:(NSNotification *)noti
{
[UIViewanimateWithDuration:2animations:^{
[selfhiddenSearchAnimation];
} completion:nil];
}
-(void)changKeyBoardAction:(NSNotification *)noti
{
if ([_nameTextField.textlength] == 0) {
[_nameTextFieldendEditing:YES];
[self.backViewremoveFromSuperview];
}
}
-(void)SearchANimation
{
[self.inputViewremoveFromSuperview];
self.inputView=[[UIViewalloc]init];
self.inputView.frame=CGRectMake(0,0, 30,30);
self.imagSearchView=[[UIImageViewalloc]init];
self.imagSearchView.image=[UIImageimageNamed:@"SearchImg"];
CGRect rx=CGRectMake(12, (30 - 15)/ 2,15, 15);
self.imagSearchView.frame=rx;
// self.inputView.backgroundColor=[UIColor blueColor];
[self.inputViewaddSubview:self.imagSearchView];
_rearchTextFiled.leftView=self.inputView;
_rearchTextFiled.leftViewMode=UITextFieldViewModeAlways;
}
-(void)InitRearchTextfiled
{
UITapGestureRecognizer *tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapAction)];
[self.viewaddGestureRecognizer:tap];
_rearchTextFiled=[[UITextFieldalloc]initWithFrame:CGRectMake(15,20, self.view.frame.size.width - 30, 30)];
_rearchTextFiled.delegate=self;
_rearchTextFiled.borderStyle=UITextBorderStyleRoundedRect;
_rearchTextFiled.placeholder=@"搜索";
_rearchTextFiled.font=[UIFontfontWithName:@"Arial"size:13.0f];
_rearchTextFiled.textColor=[UIColorblackColor];
[self.viewaddSubview:self.rearchTextFiled];
_bgView=[[UIViewalloc]initWithFrame:CGRectMake(0,_rearchTextFiled.bottom +10, self.view.frame.size.width,self.view.frame.size.height - _rearchTextFiled.bottom -10)];
_bgView.backgroundColor=[UIColorcolorWithWhite:0.5alpha:1];
[self.viewaddSubview:_bgView];
}
-(void)hiddenSearchAnimation
{
self.inputView=[[UIViewalloc]init];
CGSize sixe=[_rearchTextFiled.placeholdersizeWithFont:_rearchTextFiled.font];
CGFloat W= (_rearchTextFiled.width - sixe.width)/2;
self.inputView.frame=CGRectMake(0,0, W, 30);
UITapGestureRecognizer *inputTap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(inputTapAction)];
[self.inputViewaddGestureRecognizer:inputTap];
self.imagSearchView=[[UIImageViewalloc]init];
self.imagSearchView.image=[UIImageimageNamed:@"SearchImg"];
CGRect rx=CGRectMake(W -15, ( 30 -15)/ 2,15, 15);
self.imagSearchView.frame=rx;
[self.inputViewaddSubview:self.imagSearchView];
_rearchTextFiled.leftView=self.inputView;
_rearchTextFiled.leftViewMode=UITextFieldViewModeAlways;
}
#pragma mark --- 手势方法
-(void)inputTapAction
{
[self.rearchTextFiledbecomeFirstResponder];
}
-(void)tapAction
{
[self.rearchTextFiledresignFirstResponder];
[self.bgViewremoveFromSuperview];
}
- (void)getSearchResult:(NSString *)text {
//textFiled 改变,执行数据请求
[self.dataSourceremoveAllObjects];
NSArray *array =@[@"12345",@"12346",@"1245",@"1246",@"111",@"123",@"100",@"1"];
for (NSString *arrTextin array) {
if ([arrTexthasPrefix:text]) {
[self.dataSourceaddObject:arrText];
}
}
[self.searchTableViewreloadData];
}
#pragma tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
returnself.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier =@"SearchViewCell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
cell.textLabel.text =self.dataSource[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
二、 数据存储;
注意点: [userDefaultes arrayForKey:@"myArray"]
// NSArray --> NSMutableArray
NSMutableArray *searTXT = [myArray mutableCopy];
//缓存搜索数组
+(void)SearchText :(NSString *)seaTxt
{
NSUserDefaults *userDefaultes = [NSUserDefaultsstandardUserDefaults];
//读取数组NSArray类型的数据
NSArray *myArray = [userDefaultesarrayForKey:@"myArray"];
if (myArray.count >0) {//先取出数组,判断是否有值,有值继续添加,无值创建数组
}else{
myArray = [NSArrayarray];
}
// NSArray --> NSMutableArray
NSMutableArray *searTXT = [myArraymutableCopy];
[searTXT addObject:seaTxt];
if(searTXT.count >5)
{
[searTXT removeObjectAtIndex:0];
}
//将上述数据全部存储到NSUserDefaults中
// NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaultes setObject:searTXTforKey:@"myArray"];
[userDefaultes synchronize];
}
+(void)removeAllArray{
NSUserDefaults* userDefaults = [NSUserDefaultsstandardUserDefaults];
[userDefaults removeObjectForKey:@"myArray"];
[userDefaults synchronize];
}
三. 调用系统搜索框架
遵循代理方法
<code class="language-ruby hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#import "ViewController.h"</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@interface</span> <span class="hljs-constant" style="box-sizing: border-box;">ViewController</span> ()<<span class="hljs-constant" style="box-sizing: border-box;">UITableViewDelegate</span>,<span class="hljs-constant" style="box-sizing: border-box;">UITableViewDataSource</span>,<span class="hljs-constant" style="box-sizing: border-box;">UISearchBarDelegate</span>,<span class="hljs-constant" style="box-sizing: border-box;">UISearchResultsUpdating</span>>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@property</span> (nonatomic,strong)<span class="hljs-constant" style="box-sizing: border-box;">UITableView</span> *mainTable;
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@property</span> (nonatomic,strong)<span class="hljs-constant" style="box-sizing: border-box;">UISearchController</span> *searchController;
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@property</span> (nonatomic,strong)<span class="hljs-constant" style="box-sizing: border-box;">NSMutableArray</span> *dataList;
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@property</span> (nonatomic,strong)<span class="hljs-constant" style="box-sizing: border-box;">NSMutableArray</span> *searchList;
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@end</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
<code class="language-ruby hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">- (void)viewDidLoad {
[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];
_dataList = [<span class="hljs-constant" style="box-sizing: border-box;">NSMutableArray</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">arrayWithObjects:</span>@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"11"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"22"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"3"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"33"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"4"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"44"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ff"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"a"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aaa"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"555"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"666"</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nil</span>];
[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> searchControllerLayout];
}
-(void)searchControllerLayout{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.mainTable = [[<span class="hljs-constant" style="box-sizing: border-box;">UITableView</span> alloc]<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">initWithFrame:</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.view.bounds <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">style:</span><span class="hljs-constant" style="box-sizing: border-box;">UITableViewStylePlain</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.mainTable.delegate = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.mainTable.dataSource = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;
[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.view <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">addSubview:</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.mainTable];
_searchController = [[<span class="hljs-constant" style="box-sizing: border-box;">UISearchController</span> alloc] <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">initWithSearchResultsController:</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nil</span>];
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 输入框提示
_searchController.searchBar.placeholder=@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请输入商品名或商铺名"</span>;
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> searchResultsUpdater协议代理
_searchController.searchResultsUpdater = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 使之背景暗淡当陈述时
_searchController.dimsBackgroundDuringPresentation = <span class="hljs-constant" style="box-sizing: border-box;">NO</span>;
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = <span class="hljs-constant" style="box-sizing: border-box;">NO</span>;
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>
_searchController.searchBar.frame = <span class="hljs-constant" style="box-sizing: border-box;">CGRectMake</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.searchController.searchBar.frame.origin.x, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.searchController.searchBar.frame.origin.y, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.searchController.searchBar.frame.size.width, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44.0</span>);
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 改变系统自带的“cancel”为“取消”
[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.searchController.searchBar <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">setValue:</span>@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"取消"</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">forKey:</span>@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"_cancelButtonText"</span>];
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 表头视图为searchcontroller的searchbar
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.mainTable.tableHeaderView = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.searchController.searchBar;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul>
实现代理方法
<code class="language-ruby hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">/<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/设置区域的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return _searchList.count;
}else{
return _dataList.count;
}
}
/</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/返回单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cellFlag";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if (self.searchController.active) {
cell.textLabel.text = self.searchList[indexPath.row];
}
else{
cell.textLabel.text = self.dataList[indexPath.row];
}
return cell;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
/</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/谓词过滤
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
}
/</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/过滤数据
self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
/</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/刷新表格
[self.mainTable reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.searchController.active) {
NSLog(@"%@被选中",_searchList[indexPath.row]);
}else{
NSLog(@"%@被选中",_dataList[indexPath.row]);
}
}
</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li></ul>
实现效果如下:
![这里写图片描述](https://img-blog.csdn.net/20160328232915490)
![这里写图片描述](https://img-blog.csdn.net/20160328233000763)
五 iOS 设置自带UISearchBar的背景颜色
1、先进行图片的生成(代码生成),也可以通过UI设计师预先切好的图片。
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
UIGraphicsBeginImageContext(r.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, r);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
2、然后就可以设置了
UIImage* searchBarBg = [self GetImageWithColor:[UIColor clearColor] andHeight:32.0f];
[_searchBar setBackgroundImage:searchBarBg];
[_searchBar setBackgroundColor:[UIColor clearColor]];
[_searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
其他设置
1、设置字体颜色、默认字体颜色等
UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
searchField.textColor = [UIColor whiteColor];
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
2、修改放大镜
UIImage *image = [UIImage imageNamed:@"cl_tab2_gray"];
UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
iconView.frame = CGRectMake(0, 0, image.size.width , image.size.height);
searchField.leftView = iconView;