1.UISearchBar添加在Searchview中,searchController要对应一个viewController。
BookSearchController *searchController = [self.storyboard instantiateViewControllerWithIdentifier:@"BookSearchController"]
searchController.bookViewController = self
[self.searchView addSubview:searchController.
searchController.searchBar]

2.BookSearchController要实现
3.创建searchController,设置数据更新器,删除底图,背景透明
- (void)awakeFromNib {
searchTitles = [NSArray array];
self.searchController = [[UISearchController alloc]initWithSearchResultsController:self];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = @"搜索图书";
self.searchController.searchBar.tintColor = [UIColor whiteColor];
[self.searchController.searchBar.subviews[0].subviews[0] removeFromSuperview];
}
4.实现数据更新器的协议,搜索文字更改都会触发这个方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *tag = [searchController.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (![tag isEqualToString:@""] || !tag) {
[NetworkManager getBookTitlesWithTag:tag
page:0
resultClosure:^(NSArray * titles) {
searchTitles = titles;
[self.tableView reloadData];
}];
}
}
5.实现tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return searchTitles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]init];
cell.textLabel.text = searchTitles[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.searchController.active = NO;
self.bookViewController.tag = searchTitles[indexPath.row];
[self.bookViewController.tableView.mj_header beginRefreshing];
}