@interface SearchViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating>
{
NSMutableArray * searchList;
}
@end
@implementation SearchViewController
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if(self.searchController.active){
self.searchController.active = NO;
[self.searchController.searchBar removeFromSuperview];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.hidden = NO;
self.title = @"搜索";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navback"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(popSelf)];
//表格的创建
self.tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT,WIDTHV,HEIGHTV-NAVIGATION_BAR_HEIGHT-HOME_INDICATOR_HEIGHT)];
self.tableView.delegate=self;
self.tableView.dataSource=self;
//UISearchController的创建
self.searchController= [[UISearchController alloc]initWithSearchResultsController:nil];
//设置代理
self.searchController.delegate=self;
//取消蒙版
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchResultsUpdater=self;
self.searchController.searchBar.frame=CGRectMake(0,0,self.searchController.searchBar.frame.size.width,44.0);
[self.searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
UITextField *searchField = [self.searchController.searchBar valueForKey:@"_searchField"];
searchField.placeholder = @"搜索";
//添加到searchBar到tableView的header
self.tableView.tableHeaderView=self.searchController.searchBar;
[self.view addSubview:self.tableView];
}
-(void)popSelf
{
[self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return searchList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
static NSString *identity =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identity];
}
cell.textLabel.text = searchList[indexPath.row];
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchstring = searchController.searchBar.text;
if(searchList!=nil){
[searchList removeAllObjects];
}
//......
[self.tableView reloadData];
}
//searchcontroler 代理
- (void)didPresentSearchController:(UISearchController *)searchController
{
NSLog(@"didPresentSearchController");
self.tableView.frame =CGRectMake(0,0,WIDTHV,HEIGHTV-HOME_INDICATOR_HEIGHT);
}
@end
网友评论