iOS8之后搜索框的常规实例

时间:2021-11-19 08:38:01

1、在一个二级导航控制器中添加一个UITableviewController作为子控制器

2、UITableviewController.tableView 作为展示结果

3、利用iOS之后的UISearchController 根据输入更新输入结果


@interface WJWSearchViewController ()<UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) UITableViewController *searchTableViewController;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *searchList; @end @implementation WJWSearchViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; [self addChildViewController:self.searchTableViewController];
[self initDataArray];
[self configUI];
} - (void)initDataArray{
[self.dataArray addObject:@"runLoop"];
[self.dataArray addObject:@"gcd"];
[self.dataArray addObject:@"timer"];
[self.dataArray addObject:@"sideBar"];
[self.dataArray addObject:@"searchBar"];
[self.dataArray addObject:@"fmdb"];
[self.dataArray addObject:@"afnetworking"];
} - (void)configUI { [self.view addSubview: self.searchTableViewController.tableView];
self.searchTableViewController.tableView.frame = self.view.frame;
} #pragma mark ---UITableViewDataSource---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.searchController.active) {
return [self.searchList count];
}else {
return self.dataArray.count;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *searchCellId = @"SearchCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:searchCellId];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}else {
[cell.textLabel setText:self.dataArray[indexPath.row]];
}
return cell;
} #pragma mark ----UISearchResultsUpdating---- - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList != nil) {
[self.searchList removeAllObjects];
}
//过滤数据
self.searchList = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
//刷新表格
[self.searchTableViewController.tableView reloadData];
} -(UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = YES;//搜索框编辑时隐藏导航栏
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0);
self.searchTableViewController.tableView.tableHeaderView = _searchController.searchBar;
}
return _searchController;
} - (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
} - (UITableViewController *)searchTableViewController {
if (!_searchTableViewController) {
_searchTableViewController = [[UITableViewController alloc] init];
_searchTableViewController.tableView.delegate = self;
_searchTableViewController.tableView.dataSource = self;
}
return _searchTableViewController;
} /*
#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.
}
*/ @end

遇到的问题:

模拟器无法中文输入,

解决方法:

1、在Xcode中给项目的Scheme设置目标项目的 所属区域。默认一般使系统,可以设置为china。edit scheme-> option ->application region :china

2、在模拟器中的通用->语言地区->iPhone : 简体中文

思考:关于拼音搜索,首字母搜索,等模糊搜索未实现。