美文网首页
搜索条搜索

搜索条搜索

作者: leaderleader | 来源:发表于2017-11-01 09:19 被阅读0次

AppDelegate.m

ViewController *theVc = [[ViewController alloc]init];

UINavigationController *theNav = [[UINavigationController alloc]initWithRootViewController:theVc];

self.window.rootViewController = theNav;

ViewController.m"

<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

{

//创建表格

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

<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

{

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];

}

相关文章

  • 搜索条搜索

    AppDelegate.m ViewController *theVc = [[ViewController al...

  • IOS搜索条

    viewController.m

  • 搜索条_UISearchBar

    注意:要继承UISearchResultsUpdating,实现更新搜索结果的方法:updateSearchRes...

  • swift搜索条

    1,搜索条Options属性还可设置如下功能样式: Shows Search Results Button:勾选后...

  • Swift 搜索条

    1.定义搜索控制器变量 var searchController: UISearchController! 2.为...

  • UISearchBar 搜索条

    1。Options ShowsSearchResultsButton:勾选该复选框后,将会在搜索文本框的右端显示一...

  • UISearchBar 搜索条

    1。Options ShowsSearchResultsButton:勾选该复选框后,将会在搜索文本框的右端显示一...

  • 搜索条(UISearchBar)

  • 正版搜索条

    [self.navigationItem setHidesBackButton:YES]; //用来放search...

  • UISearchBar 搜索条

    属性 创建 barStyle————控件的样式 UISearchBarStyleProminent 用我的邮件、消...

网友评论

      本文标题:搜索条搜索

      本文链接:https://www.haomeiwen.com/subject/hxjwpxtx.html