1,创建TableView
@property(strong, nonatomic) UITableView *tableView; //表格视图
//1,创建tableview
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
//2,设置属性
self.tableView.dataSource = self; //2.1 设置数据源
self.tableView.rowHeight = 50; //2.2 设置固定行高
self.tableView.sectionIndexBackgroundColor = [UIColor grayColor]; //2.3 修改右侧指示栏背景颜色
self.tableView.sectionIndexTrackingBackgroundColor = [UIColor lightGrayColor]; //2.4 修改右侧指示栏背景颜色(点击时)
self.tableView.sectionIndexColor = [UIColor blackColor]; //2.5 修改右侧指示栏标题颜色
self.tableView.delegate = self; //2.6 设置代理
//3,添加到父视图
[self.view addSubview:self.tableView];
2,ResultTableViewController创建
//4,创建收索控制器
ResultTableViewController *resultVC = [[ResultTableViewController alloc] init]; //4.1 创建结果控制器
self.searchController = [[UISearchController alloc]initWithSearchResultsController:resultVC]; //4.2 创建收索控制器
[self.searchController.searchBar sizeToFit]; //4.3 和tabview头部进行关联
self.tableView.tableHeaderView = self.searchController.searchBar; //4.4 自适应大小
self.definesPresentationContext = YES; //4.5 找到searchController正确的位置
self.searchController.dimsBackgroundDuringPresentation = NO; //4.6 呈现时取消背景
self.searchController.searchResultsUpdater = self; //4.7 设置searchResultsUpdater
3,UITabkeViewDelegate
#pragma mark - UITabkeViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// 配置每一行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
// 配置区段的头部显示文本
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.keys[section];
}
// 配置右侧指示栏标题列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSLog(@"%@",_keys);
return self.keys;
}