#import "SelectViewController.h"
@interface SelectViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate>
{
BOOL shouldShowSearchResults;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchController * searchController;
@property (nonatomic, strong) NSMutableArray * carNumberArray; //汽车牌号
@property (nonatomic, strong) NSMutableArray * filteredArray;
@property (nonatomic, strong) NSArray * titleArray;
@end
@implementation SelectViewController
- (NSArray *)titleArray {
if (!_titleArray) {
_titleArray = @[];
}
return _titleArray;
}
- (NSMutableArray *)carNumberArray {
if (!_carNumberArray) {
_carNumberArray = [@[] mutableCopy];
}
return _carNumberArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
self.view.backgroundColor = [UIColor clearColor];
[self configureSearchController];
}
- (void)configureSearchController {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.searchBar.placeholder = @"";
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
[_searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = _searchController.searchBar;
}
#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
shouldShowSearchResults = YES;
[self.tableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
shouldShowSearchResults = NO;
[self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (!shouldShowSearchResults) {
shouldShowSearchResults = YES;
[self.tableView reloadData];
}
[self.searchController.searchBar resignFirstResponder];
}
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString * searchString = searchController.searchBar.text;
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchString];
self.filteredArray = [[self.carNumberArray filteredArrayUsingPredicate:predicate] mutableCopy];
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return shouldShowSearchResults ? self.filteredArray.count : self.carNumberArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellId = @"car_num_cell_id";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if (shouldShowSearchResults) {
cell.textLabel.text = self.filteredArray[indexPath.row];
}else {
cell.textLabel.text = self.carNumberArray[indexPath.row];
}
cell.imageView.image = [UIImage imageNamed:@"car.png"];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (shouldShowSearchResults) {
}else {
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UISearchController的基本使用
最新推荐文章于 2018-11-12 23:00:00 发布