#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
// 展示所有数据的表格视图
@property(strong) UITableView* tableView;
// 表格的数据源
@property(strong) NSMutableArray* dataSource;
// 存放搜索结果的数组
@property(strong) NSMutableArray* resultArray;
// 类似文本框的控件,专门用于搜索功能
@property(strong) UISearchBar* searchBar;
// 搜索控制器
@property(strong) UISearchDisplayController* searchDisplayCtrl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.automaticallyAdjustsScrollViewInsets = NO;
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
// 构建一个分组样式的表视图
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480) style:UITableViewStylePlain];
// 构建表格视图与控制器的委托关系
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// 实例化搜索框控件,默认高度是44
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[_searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
//[self.view addSubview:_searchBar];
_tableView.tableHeaderView = _searchBar;
// 如果想使用searchBar的回调方法,也要遵守协议并设置委托对象
// _searchBar.delegate = self;
/*
第一个参数,是一个UISearchBar对象,用于输入搜索信息
第二个参数,是一个视图控制器对象,这个控制器对象必须遵守表格视图的两个协议(delegate,dataSource)
UISearchDisplayController对象实际上自带一个表格视图searTableView,这个表格视图负责加载当前搜索的信息,这些信息由第二个参数负责帮忙展示出来,所以 第二个参数一定是遵守表格视图的两个协议的
【注】当前有两个表格视图,一个是我们自己构建的那个_tableView 还有一个是搜索器自带的searchTableView,这两个表格视图共用一个视图控制器(self)
*/
_searchDisplayCtrl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
// 设置搜索控制器的委托(实际上就是设置搜索控制器自带的表格视图的委托对象)两个视图都要跟self构建委托关系,self才能帮两个表格视图加载数据
[_searchDisplayCtrl setSearchResultsDataSource:self];
[_searchDisplayCtrl setSearchResultsDelegate:self];
// 添加数据源
[self createSource];
}
- (void)createSource
{
// 搜索结果数组的初始化
_resultArray = [[NSMutableArray alloc] init];
// 表格视图数据源的初始化
_dataSource = [[NSMutableArray alloc] init];
for (int i = 'A'; i <= 'Z'; i++) {
NSMutableArray* tempArr = [[NSMutableArray alloc] init];
for (int j = 0; j < 10; j++) {
NSString* str = [NSString stringWithFormat:@"%c%d",i,j];
// 先将一组数据添加到一个数组里
[tempArr addObject:str];
}
// 再把一组数据添加到数据源里
[_dataSource addObject:tempArr];
}
}
#pragma mark- UITableViewDataSource
// 设置表格的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 区分当前要展示数据的表格视图是搜索器视图还是加载数据源的视图对象
// 搜索器自带的表视图是在搜索器的搜索框被输入内容的时候开始展示视图的
if (tableView != _tableView) {
// 如果不相等,说明当前这个表格视图是搜索器自带的那个表视图,这个视图不需要分区展示,所以返回1
return 1;
}else
// 如果else 说明当前的表格视图是加载数据源的表视图,所以返回数据源的分区数
return _dataSource.count;
}
// 设置表格每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 判断当前展示数据的表视图是不是要展示数据源的那个表视图
if (tableView != _tableView) {
// 先把上次搜索的临时数据清空
[_resultArray removeAllObjects];
// 获取当前搜索框内的信息
NSString* searchMessage = _searchBar.text;
for (NSArray* tempArr in _dataSource) {
for (NSString* data in tempArr) {
// 查看每一个数据项内是否包含当前搜索框内的字符串
NSRange range = [data rangeOfString:searchMessage];
if (range.location != NSNotFound) {
[_resultArray addObject:data];
}
}
}
// 如果不是,则要展示的是当前的搜索结果
return _resultArray.count;
}else
// 数据源每个分区要显示的行数
return [[_dataSource objectAtIndex:section] count];
}
// 设置表格每一行要显示的内容
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* str = @"gczCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
// 设置cell内容
if (tableView != _tableView) {
// 如果是展示搜索结果的表视图,则加载搜索结果的内容
cell.textLabel.text = [_resultArray objectAtIndex:indexPath.row];
}else{
// 如果是展示数据源数据的表视图,则加载数据源的数据
cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
return cell;
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"%c分区",'A'+section];
}
// 返回索引标题的方法,把所有标题放到一个数组里作为返回值
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray* arr = [[NSMutableArray alloc] init];
for (int i='A'; i<'Z'; i++) {
NSString* title = [NSString stringWithFormat:@"%c",i];
[arr addObject:title];
}
return arr;
}
// 返回当前选中分区索引,表示图会把这个分区内容显示出来
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSLog(@"index:%ld,title:%@",(long)index,title);
return index;
}
@end
tableView搜索
最新推荐文章于 2022-10-28 09:41:58 发布