UISearchBar搜索控制器
来源:互联网 发布:windows xp镜像下载 编辑:程序博客网 时间:2024/06/10 00:09
<UITableViewDataSource,UITableViewDelegate>两个协议
全局变量
{
//表格式图
UITableView * _tableView;
//搜索条
UISearchBar* _searchBar;
//搜索控制器
UISearchDisplayController * _seachDisplay;
//展示的数据
NSMutableArray * dataArray;
//展示搜索的数据源
NSMutableArray * resultArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initWithData];
[self createSearch];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)initWithData
{
dataArray=[[NSMutableArray alloc]init];
resultArray=[[NSMutableArray alloc]init];
for (int i='A'; i<='Z'; i++)
{
NSMutableArray* subArray=[NSMutableArray array];
for (int j=0; j<10; j++)
{
NSString * string=[NSString stringWithFormat:@"%c %d",i,j];
[subArray addObject:string];
}
[dataArray addObject:subArray];
}
}
#pragma mark -----搜索方法-----
-(void)createSearch
{
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource=self;
_tableView.delegate=self;
[self.view addSubview:_tableView];
//创建搜索条
_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
//将其添加到表格头上
_tableView.tableHeaderView=_searchBar;
//创建搜索控制器
_seachDisplay=[[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
//其上边自带了一个表格式图
_seachDisplay.searchResultsDelegate=self;
_seachDisplay.searchResultsDataSource=self;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView!=_tableView) {
return @"搜索结果";
}
return [NSString stringWithFormat:@"%c",(unichar)('A'+section)];
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray * title=[[NSMutableArray alloc]init];
//添加小标志
[title addObject:UITableViewIndexSearch];
for (int i='A'; i<='Z'; i++) {
[title addObject:[NSString stringWithFormat:@"%c",i]];
}
return title;
}
//设置一下对应关系
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//index 0 -26 搜索-Z 分组0-25
if (index==0) {
//点击搜索条时 将tableView 滑到搜索条的位置
[_tableView scrollRectToVisible:_searchBar.frame animated:YES];
return -1;
}
return index-1;
}
#pragma mark -----协议方法-----
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//搜索控制器上自带的表格式图
if (tableView!=_tableView)
{
return 1;
}
return dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView!=_tableView)
{
//返回搜索结果的值
//现将之前搜索的内容 移除
[resultArray removeAllObjects];
for (NSArray * subArray in dataArray)
{
for (NSString * string in subArray)
{
NSRange range=[string rangeOfString:_searchBar.text];
if (range.location!=NSNotFound)
{
[resultArray addObject:string];
}
}
}
return resultArray.count;
}
return [[dataArray objectAtIndex:section]count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellId=@"cellId";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if (tableView!=_tableView)
{
cell.textLabel.text=resultArray[indexPath.row];
}else
{
cell.textLabel.text=dataArray[indexPath.section][indexPath.row];
}
return cell;
}
- UISearchBar搜索控制器
- UISearchBar搜索
- UISearchBar搜索
- UISearchBar 搜索框
- UISearchBar、UISearchDisplayController搜索
- UISearchBar搜索框用法
- iOS搜索框UISearchBar
- UI UISearchBar搜索框
- iOS UISearchBar 搜索功能
- UISearchBar 搜索框
- UISearchBar 搜索框
- uisearchBar 支持空搜索
- UISearchBar 支持空搜索
- uisearchbar支持空搜索
- IOS-模糊搜索UISearchBar+UISearchDisplayController
- 让 UISearchBar 支持空搜索
- 搜索栏UISearchBar和UISearchController
- 搜索栏UISearchBar和UISearchController
- Keystone 用户认证
- block
- ios学习之苹果官方文档的学习(EKCalendarChooserDelegate)
- UFT中如何在一个Action中自动建表并填写数据
- iOS之OC基础类和对象
- UISearchBar搜索控制器
- 文件流的介绍
- 1-环形矩阵
- 万用表
- Genymotion 神器安装及问题处理,请我们都使用神器吧
- BC#62C题求二叉树不同节点数的子树的个数
- UISegmentedControl分段栏
- Git
- 哈理工acm练习赛 K - Tangled in Cables