UISearchController在iOS8.0之后替换了原来的UISearchBar,这里介绍一下搭配UITableView的使用;
先上图实现效果如下:
1.xib创建一个VC,并在上面添加一个UITableView添加约束,内容页代码如下:
@interface ApplicationViewController ()<UISearchResultsUpdating>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UISearchController *searchController;
@end
@implementation ApplicationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBar.translucent = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
[self.tableView registerNib:[UINib nibWithNibName:@"ApplicationTableViewCell" bundle:nil] forCellReuseIdentifier:@"ApplicationTableViewCell"];
_tableView.tableHeaderView = self.searchController.searchBar;
_searchController.searchResultsUpdater = self;
self.definesPresentationContext = YES;
}
//懒加载搜索控制器
- (UISearchController *)searchController {
if (!_searchController) {
SearchResultTableViewController *result = [SearchResultTableViewController new];
_searchController = [[UISearchController alloc] initWithSearchResultsController:result];
_searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
_searchController.searchBar.placeholder = @"搜索";
_searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchController.searchBar.barTintColor = [UIColor whiteColor];
return _searchController;
}
return _searchController;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ApplicationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplicationTableViewCell"];
cell.selectionStyle = NO;
return cell;
}
#pragma mark UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController; {
}
@end
2.创建一个继承与UITableViewController的类作为显示搜索内容的VC
#import "SearchResultTableViewController.h"
@interface SearchResultTableViewController ()
@end
@implementation SearchResultTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}