搜索条(在表格中)实现搜索功能
来源:互联网 发布:新网域名查询 编辑:程序博客网 时间:2024/04/24 18:37
/*此代码可通过首字母实现搜索*/<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;}- (void)viewDidLoad { [super viewDidLoad]; //模拟数据 // _allData = [NSArray arrayWithArray:[UIFont familyNames]]; _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; }}//cell-(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];}
阅读全文
0 0
- 搜索条(在表格中)实现搜索功能
- 在表格中实现搜索
- JS实现表格数据各种搜索功能
- flex 在表格(DataGrid)中搜索
- easyUI下拉框表格扩展(实现搜索功能)[easyUI+java]
- 使用Vue.js实现表格的排序和搜索功能
- windows系统中搜索功能MFC实现
- Jsp中实现搜索查找功能
- iOS中tableView上实现搜索功能
- Android开发中搜索功能的实现
- MUI框架中实现附近搜索功能
- ios UITableView表格实现搜索栏搜索
- sql实现搜索功能
- 实现搜索功能
- sharepoint:实现搜索功能
- 搜索功能的实现
- Ajax实现搜索功能
- 实现搜索功能
- Oracle 查看表空间的大小及使用情况sql语句
- Golang基本工具的安装以及cobra的安装
- 逻辑覆盖
- CCF CSP 2016年9月第3题 炉石传说(模拟)
- SSL2817 2017年11月1日提高组 树论(并查集)
- 搜索条(在表格中)实现搜索功能
- python3.X 和python2.X的几点小区别
- 父元素高度自适应,子元素浮动,引起的高度塌陷问题
- 清除浮动
- C Primer Plus 第六版 所有章节课后编程练习答案
- Ubuntu 16.04 ssh 登陆出现错误Algorithm Negotiation failed when trying to connect to server 解决
- gulp JS文件合并和压缩
- 常用命令:su / sudo
- B1030. 完美数列