AppDelegate.m
ViewController *theVc = [[ViewController alloc]init];
UINavigationController *theNav = [[UINavigationController alloc]initWithRootViewController:theVc];
self.window.rootViewController = theNav;
ViewController.m"
{
//创建表格
UITableView *theTableView;
//创建数组
NSMutableArray *theArray;
NSMutableArray *theNewArray;
//创建搜索条
UISearchBar *theSearchBar;
}
//========================
////创建导航标题
self.title = @"搜索条";
//创建搜索条控件并设置位置
theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
//添加协议
theSearchBar.delegate = self;
//显示取消按钮
theSearchBar.showsCancelButton = YES;
// //添加取消按钮(焦点)
// [theSearchBar becomeFirstResponder];
[self.view addSubview:theSearchBar];
//初始化表格
theTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height)];
//添加表格协议
theTableView.dataSource = self;
theTableView.delegate = self;
//添加到视图上
[self.view addSubview:theTableView];
//1 4
theArray = [NSMutableArray arrayWithObjects:@"张浩宁",@"李凯",@"井秋香",@"王剑",@"王一鸣",@"王瑞宇", nil];
theNewArray = [theArray mutableCopy];
//=======================
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return theArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
//创建复用池
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
//创建正标题
cell.textLabel.text = theArray[indexPath.row];
return cell;
}
#pragma -
#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self showdata];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
if([searchBar canResignFirstResponder])
[searchBar resignFirstResponder];
[self showdata];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self search:searchText];
}
#pragma mark -
#pragma mark private methods
-(void)showdata{
theArray = [theNewArray mutableCopy];
[theTableView reloadData];
}
-(void)search:(NSString*)searchtext{
NSMutableArray *tmp = [NSMutableArray array];
//筛选数据 把输入的字母全部转换为大写(让判断不区分大小写)
NSString *str = [theSearchBar.text uppercaseString];
//遍历人名数组 哈希算法:hasPrefix
for (NSString *s in theArray) {
//判断是否以输入的字母开头
if([[s uppercaseString] hasPrefix:str] )
{
[tmp addObject:s];
}
}
//将新的内容添加到新的数组中并同时赋值给theArray这个数组对象
theArray = [NSMutableArray arrayWithArray:tmp];
//刷新表格
[theTableView reloadData];
}
//==============================================第二种方法 完善======================================
ViewController.m
{
NSArray *_allData;
//搜索条
UISearchBar *search;
//搜索控制器
UISearchDisplayController *displayCon;
//搜索结果
NSArray *_resultArr;
}
@property (nonatomic,strong)UITableView *table;
//重写get方法
-(UITableView *)table{
if (!_table) {
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStylePlain];
_table.dataSource = self;
_table.delegate = self;
}
return _table;
}
//===================================
//模拟数据
_allData = [NSArray arrayWithObjects:@"惊奇",@"许大川",@"王剑",@"杜建军",@"小鹏", nil];
[self.view addSubview:self.table];
//初始化搜索条
search = [[UISearchBar alloc]init];
//设置提示文字
search.placeholder = @"搜索姓名";
search.delegate = self;
//初始化搜索控制器
displayCon = [[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];
//设置代理
displayCon.searchResultsDataSource =self;
displayCon.searchResultsDelegate =self;
//设置头视图
self.table.tableHeaderView = search;
// _resultArr = [[NSMutableArray alloc]init];
//===================================
//返回多少行数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.table == tableView) {
return _allData.count;
}else{
return _resultArr.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
if (self.table == tableView) {
cell.textLabel.text = _allData[indexPath.row];
}else{
cell.textLabel.text = [_resultArr objectAtIndex:indexPath.row];
}
return cell;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSMutableArray *tmp = [NSMutableArray array];
//筛选数据 把输入的字母全部转换为大写(让判断不区分大小写)
NSString *str = [search.text uppercaseString];
//遍历人名数组
for (NSString *s in _allData) {
//判断是否以输入的字母开头
if([[s uppercaseString] hasPrefix:str] ){
[tmp addObject:s];
}
}
_resultArr = [NSArray arrayWithArray:tmp];
[self.table reloadData];
}