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

时间:2023-03-10 04:42:15
iOS:搜索栏控件UISearchBar and SearchDisplayController的使用

UISearchBar and SearchDisplayController控件:

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

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

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

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

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

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

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

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

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

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

具体代码如下:

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

Contact.h

 #import <Foundation/Foundation.h>

 @interface Contact : NSObject
@property (copy,nonatomic)NSString *name;
@property (copy,nonatomic)NSString *telphone;
-(instancetype)initWithName:(NSString*)name andTelphone:(NSString*)telphone;
@end

Contact.m

 #import "Contact.h"

 @implementation Contact
-(instancetype)initWithName:(NSString*)name andTelphone:(NSString*)telphone
{
self = [super init];
if(self)
{
_name = [name copy];
_telphone = [telphone copy];
}
return self;
}
@end

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

ViewController.h

 #import <UIKit/UIKit.h>

 @class Contact;
@interface ViewController : UIViewController
@property (strong,nonatomic)Contact *contact;
@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchVC;//搜索栏控制器
@property (strong,nonatomic)NSArray *searchedResults;//搜索栏表格数据数组
@property (strong,nonatomic)NSMutableArray *contacts;//当前控制器表格数据数组
@end

ViewController.m

 #import "ViewController.h"
#import "Contact.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;//表格视图
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;//搜索栏
@property (assign,nonatomic)BOOL isSearched;//判断是搜索栏的表格视图,还是视图控制器的,刷新数据
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; //初始化
self.contacts = [NSMutableArray arrayWithCapacity:]; //创建是个联系人
for(int i=; i<; i++)
{
self.contact = [[Contact alloc]initWithName:[NSString stringWithFormat:@"contact%02d",i] andTelphone:[NSString stringWithFormat:@"1873456%04d",arc4random_uniform()]]; [self.contacts addObject:self.contact];
} //设置tableView数据源和代理
self.tableView.dataSource = self;
self.tableView.delegate = self; //设置UISearchBar代理
self.searchBar.delegate = self; //初始化为NO
self.isSearched = NO;
}
//视图显示时,刷新数据
-(void)viewWillAppear:(BOOL)animated
{
if(self.isSearched)
{
[self.searchVC.searchResultsTableView reloadData];//搜索栏控制器的表格视图刷新数据
}
else
{
[self.tableView reloadData];//当前视图控制器的表格视图刷新数据
}
}
#pragma mark -<UITableViewDataSource>
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.tableView)
{
self.isSearched = NO;
return self.contacts.count;
}
else
{
self.isSearched = YES;
return self.searchedResults.count;
}
}
//设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.如果没有找到,自己创建单元格对象
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
//3.设置单元格对象的内容
if(tableView == self.tableView)
{
self.contact = self.contacts[indexPath.row];
cell.textLabel.text = self.contact.name;
cell.detailTextLabel.text = self.contact.telphone;
}
else
{
self.contact = self.searchedResults[indexPath.row];
cell.textLabel.text = self.contact.name;
cell.detailTextLabel.text = self.contact.telphone;
}
return cell;
}
#pragma mark -<UITableViewDElegate>
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} #pragma mark -<UISearchBarDelegate>
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
//隐藏导航栏
[self.navigationController setNavigationBarHidden:YES];
return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
//显示导航栏
[self.navigationController setNavigationBarHidden:NO];
return YES;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
//刷新表格
[self.tableView reloadData];
}
#pragma mark -<UISearchDisplayDelegate>
//使用搜索字符串过滤原始数据,找出符合条件的联系人
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
//谓词的格式化
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd]%@",searchString]; //过滤原始数据
self.searchedResults = [self.contacts filteredArrayUsingPredicate:predicate]; return YES;
}
@end