AppDelegate.m
ViewController *theVc = [[ViewController alloc]init];
UINavigationController *theNav = [[UINavigationController alloc]initWithRootViewController:theVc];
self.window.rootViewController = theNav;
ViewController.m"
<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
//創(chuàng)建表格
UITableView? ? *theTableView;
//創(chuàng)建數(shù)組
NSMutableArray *theArray;
NSMutableArray *theNewArray;
//創(chuàng)建搜索條
UISearchBar *theSearchBar;
}
//========================
////創(chuàng)建導(dǎo)航標(biāo)題
self.title = @"搜索條";
//創(chuàng)建搜索條控件并設(shè)置位置
theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
//添加協(xié)議
theSearchBar.delegate = self;
//顯示取消按鈕
theSearchBar.showsCancelButton = YES;
//? ? //添加取消按鈕(焦點(diǎn))
//? ? [theSearchBar becomeFirstResponder];
[self.view addSubview:theSearchBar];
//初始化表格
theTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height)];
//添加表格協(xié)議
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:@""];
//創(chuàng)建復(fù)用池
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
//創(chuàng)建正標(biāo)題
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];
//篩選數(shù)據(jù)? 把輸入的字母全部轉(zhuǎn)換為大寫(讓判斷不區(qū)分大小寫)
NSString *str = [theSearchBar.text uppercaseString];
//遍歷人名數(shù)組 哈希算法:hasPrefix
for (NSString *s in theArray) {
//判斷是否以輸入的字母開頭
if([[s uppercaseString] hasPrefix:str] )
{
[tmp addObject:s];
}
}
//將新的內(nèi)容添加到新的數(shù)組中并同時(shí)賦值給theArray這個(gè)數(shù)組對象
theArray = [NSMutableArray arrayWithArray:tmp];
//刷新表格
[theTableView reloadData];
}
//==============================================第二種方法 完善======================================
ViewController.m
<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
NSArray *_allData;
//搜索條
UISearchBar *search;
//搜索控制器
UISearchDisplayController *displayCon;
//搜索結(jié)果
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;
}
//===================================
//模擬數(shù)據(jù)
_allData = [NSArray arrayWithObjects:@"驚奇",@"許大川",@"王劍",@"杜建軍",@"小鵬", nil];
[self.view addSubview:self.table];
//初始化搜索條
search = [[UISearchBar alloc]init];
//設(shè)置提示文字
search.placeholder = @"搜索姓名";
search.delegate = self;
//初始化搜索控制器
displayCon = [[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];
//設(shè)置代理
displayCon.searchResultsDataSource =self;
displayCon.searchResultsDelegate =self;
//設(shè)置頭視圖
self.table.tableHeaderView = search;
//? ? _resultArr = [[NSMutableArray alloc]init];
//===================================
//返回多少行數(shù)據(jù)
-(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];
//篩選數(shù)據(jù)? 把輸入的字母全部轉(zhuǎn)換為大寫(讓判斷不區(qū)分大小寫)
NSString *str = [search.text uppercaseString];
//遍歷人名數(shù)組
for (NSString *s in _allData) {
//判斷是否以輸入的字母開頭
if([[s uppercaseString] hasPrefix:str] ){
[tmp addObject:s];
}
}
_resultArr = [NSArray arrayWithArray:tmp];
[self.table reloadData];
}