一、UISearchController在UITableView的使用
1、在放初始化的搜索框中的控制器代码。
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
[self initData];
}
/**
初始化UI
*/
- (void)initUI{
//必须要将这个属性设置成YES,不然会在搜索结果的界面有64px的偏移
self.definesPresentationContext = YES;
SearchResultController * vc = [[SearchResultController alloc] init];
//设置搜索的控制器
self.searchController = [[UISearchController alloc] initWithSearchResultsController:vc];
//当呈现的时候是否隐藏导航栏
self.searchController.hidesNavigationBarDuringPresentation = YES;
//点击searchBar的输入框的时候是否显示灰色的背景
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.searchBar.placeholder = @"请输入关键字";
self.searchController.searchBar.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
/**
初始化数据源
*/
- (void)initData{
self.dataArray = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"country" ofType:@"txt"];
NSString *countriesContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
self.dataArray = [countriesContent componentsSeparatedByString:@"\n"];
}
#pragma mark - UISearchControllerUpdate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchText = searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF CONTAINS %@)", searchText];
SearchResultController *resultViewCtrl = (SearchResultController *)searchController.searchResultsController;
NSArray * arr = [_dataArray filteredArrayUsingPredicate:predicate];
resultViewCtrl.filterArr = arr;
}
#pragma mark - UISearchController delegate method
- (void)willPresentSearchController:(UISearchController *)searchController {
}
- (void)didPresentSearchController:(UISearchController *)searchController {
}
- (void)presentSearchController:(UISearchController *)searchController {
}
- (void)willDismissSearchController:(UISearchController *)searchController {
}
- (void)didDismissSearchController:(UISearchController *)searchController {
}
#pragma mark - UISearchBar delegate method
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
NSLog(@"%s", __func__);
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"%s", __func__);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
2、在搜索结果中的代码。
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
[self initData];
}
/**
初始化UI
*/
- (void)initUI{
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
}
/**
初始化数据
*/
- (void)initData{
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.filterArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
cell.textLabel.text = self.filterArr[indexPath.row];
return cell;
}
- (void)setFilterArr:(NSMutableArray *)filterArr{
_filterArr = filterArr;
[self.tableView reloadData];
}
二、UISearchController添加到UIScrollView上
将这个空间添加到self.view上会出现问题
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.eoc_width, self.view.eoc_height)];
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
[scrollView addSubview:self.searchCtrl.searchBar];
- (UISearchController *)searchCtrl {
if (!_searchCtrl) {
EOCSearchResultViewCtrl *resultSearchCtrl = [[EOCSearchResultViewCtrl alloc] init];
_searchCtrl = [[UISearchController alloc] initWithSearchResultsController:resultSearchCtrl];
_searchCtrl.dimsBackgroundDuringPresentation = YES;
_searchCtrl.hidesNavigationBarDuringPresentation = YES;
_searchCtrl.searchBar.placeholder = @"搜索";
_searchCtrl.searchBar.delegate = self;
_searchCtrl.searchResultsUpdater = self;
_searchCtrl.delegate = self;
}
return _searchCtrl;
}
三、将UISearchController添加写成成员变量,代理方法不能不执行。原因:提前释放掉这个对象了。
self.view.backgroundColor = [UIColor whiteColor];
EOCSearchResultViewCtrl * vc = [[EOCSearchResultViewCtrl alloc] init];
UISearchController * searchVC = [[UISearchController alloc] initWithSearchResultsController:vc];
searchVC.searchBar.placeholder = @"搜索";
searchVC.dimsBackgroundDuringPresentation = YES;
searchVC.hidesNavigationBarDuringPresentation = YES;
searchVC.searchBar.placeholder = @"八点钟学院";
searchVC.searchBar.delegate = self;
searchVC.searchResultsUpdater = self;
四、改变UISearchController中searchBar的外观
//修改searchBar中的背景色
self.searchCtrl.searchBar.barTintColor = [UIColor grayColor];
//修改searchBar的光标和cancel的颜色
self.searchCtrl.searchBar.tintColor = [UIColor orangeColor];
//改变searchBar中的搜索图标
[self.searchCtrl.searchBar setImage:[UIImage imageNamed:@"voice_gray"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
//改变searchBar中清楚按钮的图标
[self.searchCtrl.searchBar setImage:[UIImage imageNamed:@"message_gray"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
//改变searchBar中清楚按钮高亮状态中的图标
[self.searchCtrl.searchBar setImage:[UIImage imageNamed:@"message_gray"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
//改变输入框的背景色和文字颜色
for (UIView *subView in self.searchVC.searchBar.subviews){
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass:[UITextField class]]){
UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
searchBarTextField.backgroundColor = [UIColor colorWithHex:0x8C8F93 alpha:1];
UIColor * color = [UIColor colorWithHex:0x607AA8 alpha:1];
searchBarTextField.attributedText = [[NSAttributedString alloc] initWithString:@"搜索" attributes:@{NSForegroundColorAttributeName:color}];
break;
}
}
}
[self.searchVC.searchBar setImage:[UIImage imageNamed:@"dev_search-1"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
//一下代码为修改placeholder字体的颜色和大小
UITextField * searchField = [self.searchVC.searchBar valueForKey:@"_searchField"];
[searchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[searchField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
48:10 3.29