您现在的位置是:首页 > 文章详情

iOS:搜索栏控件UISearchBar and SearchDisplayController的使用

日期:2017-06-28点击:356

UISearchBar and SearchDisplayController控件:

这是一个带搜索栏和搜索显示控制器的控件,前面的SearchBar是一个搜索栏,它提供一个输入搜索条件的类似于文本框的东西,后面的SearchDisplayController是一个显示搜索结果的控制器,它自带着一个searchResultsTableView搜索结果表格视图,用来显示搜索的结果的。当然,这个表格视图要想实现功能,必须要实现<UISearchBarDelegate>和<UISearchDisplayDelegate>协议。输入搜索条件时,要用到一个新知识,即谓词NSPredicate对象的使用,它类似于数据库的搜索,也用到类似于正则表达式的功能。

 

用途举例:搜索当前表格中某一类型的数据。这就涉及到了两个表格视图,一个TableView和另一个searchResultsTableView,因此,需要一个区分标识,以用来决定需要显示那一个表格视图的内容。

 

提示:该控件在iOS8中已经过时,被UISearchController取代。

 

尽管如此,我还是用UISearchBar and SearchDisplayController控件举一个例子如下:

1、没有搜索时: 2.点击搜索栏时:

  

3.显示搜索结果: 4.取消搜索时:

  

 

所有文件和storyboard布局截图如下:

 

具体代码如下:

1.创建联系人类并初始化对象

Contact.h

复制代码
1 #import <Foundation/Foundation.h> 2 3 @interface Contact : NSObject 4 @property (copy,nonatomic)NSString *name; 5 @property (copy,nonatomic)NSString *telphone; 6 -(instancetype)initWithName:(NSString*)name andTelphone:(NSString*)telphone; 7 @end
复制代码

Contact.m

复制代码
 1 #import "Contact.h"  2  3 @implementation Contact  4 -(instancetype)initWithName:(NSString*)name andTelphone:(NSString*)telphone  5 {  6 self = [super init];  7 if(self)  8  {  9 _name = [name copy]; 10 _telphone = [telphone copy]; 11  } 12 return self; 13 } 14 @end
复制代码

2.在视图控制器类中实现显示表格和搜索显示功能

ViewController.h

复制代码
1 #import <UIKit/UIKit.h> 2 3 @class Contact; 4 @interface ViewController : UIViewController 5 @property (strong,nonatomic)Contact *contact; 6 @property (strong, nonatomic) IBOutlet UISearchDisplayController *searchVC;//搜索栏控制器 7 @property (strong,nonatomic)NSArray *searchedResults;//搜索栏表格数据数组 8 @property (strong,nonatomic)NSMutableArray *contacts;//当前控制器表格数据数组 9 @end
复制代码

ViewController.m

复制代码
 1 #import "ViewController.h"  2 #import "Contact.h"  3  4 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>  5 @property (weak, nonatomic) IBOutlet UITableView *tableView;//表格视图  6 @property (weak, nonatomic) IBOutlet UISearchBar *searchBar;//搜索栏  7 @property (assign,nonatomic)BOOL isSearched;//判断是搜索栏的表格视图,还是视图控制器的,刷新数据  8 @end  9  10 @implementation ViewController  11  12 - (void)viewDidLoad {  13  [super viewDidLoad];  14  15 //初始化  16 self.contacts = [NSMutableArray arrayWithCapacity:10];  17  18 //创建是个联系人  19 for(int i=0; i<10; i++)  20  {  21 self.contact = [[Contact alloc]initWithName:[NSString stringWithFormat:@"contact%02d",i] andTelphone:[NSString stringWithFormat:@"1873456%04d",arc4random_uniform(10000)]];  22  23  [self.contacts addObject:self.contact];  24  }  25  26 //设置tableView数据源和代理  27 self.tableView.dataSource = self;  28 self.tableView.delegate = self;  29  30 //设置UISearchBar代理  31 self.searchBar.delegate = self;  32  33 //初始化为NO  34 self.isSearched = NO;  35 }  36 //视图显示时,刷新数据  37 -(void)viewWillAppear:(BOOL)animated  38 {  39 if(self.isSearched)  40  {  41 [self.searchVC.searchResultsTableView reloadData];//搜索栏控制器的表格视图刷新数据  42  }  43 else  44  {  45 [self.tableView reloadData];//当前视图控制器的表格视图刷新数据  46  }  47 }  48 #pragma mark -<UITableViewDataSource>  49 //行数  50 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  51 {  52 if(tableView == self.tableView)  53  {  54 self.isSearched = NO;  55 return self.contacts.count;  56  }  57 else  58  {  59 self.isSearched = YES;  60 return self.searchedResults.count;  61  }  62 }  63 //设置每一个单元格的内容  64 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  65 {  66 //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象  67 static NSString *reuseIdentifier = @"Cell";  68 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];  69 //2.如果没有找到,自己创建单元格对象  70 if(cell == nil)  71  {  72 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];  73  }  74 //3.设置单元格对象的内容  75 if(tableView == self.tableView)  76  {  77 self.contact = self.contacts[indexPath.row];  78 cell.textLabel.text = self.contact.name;  79 cell.detailTextLabel.text = self.contact.telphone;  80  }  81 else  82  {  83 self.contact = self.searchedResults[indexPath.row];  84 cell.textLabel.text = self.contact.name;  85 cell.detailTextLabel.text = self.contact.telphone;  86  }  87 return cell;  88 }  89 #pragma mark -<UITableViewDElegate>  90 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  91 {  92 return 55;  93 }  94  95 #pragma mark -<UISearchBarDelegate>  96 -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar  97 {  98 //隐藏导航栏  99  [self.navigationController setNavigationBarHidden:YES]; 100 return YES; 101 } 102 -(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar 103 { 104 //显示导航栏 105  [self.navigationController setNavigationBarHidden:NO]; 106 return YES; 107 } 108 -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 109 { 110 //刷新表格 111  [self.tableView reloadData]; 112 } 113 #pragma mark -<UISearchDisplayDelegate> 114 //使用搜索字符串过滤原始数据,找出符合条件的联系人 115 -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 116 { 117 //谓词的格式化 118 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd]%@",searchString]; 119 120 //过滤原始数据 121 self.searchedResults = [self.contacts filteredArrayUsingPredicate:predicate]; 122 123 return YES; 124 } 125 @end
复制代码

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!


本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4823169.html,如需转载请自行联系原作者
原文链接:https://yq.aliyun.com/articles/362684
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章