#import "RootController.h"
@interface RootController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>
@property (nonatomic, retain) NSMutableArray *dataList; // 存储数据源
@property (nonatomic, retain) NSMutableArray *searchList; // 存储搜索结果
@property (nonatomic, retain) UISearchController *searchController; // 管理搜索框
@end
@implementation RootController
- (void)dealloc {
self.dataList = nil;
self.searchList = nil;
self.searchController = nil;
[super dealloc];
}
// 懒加载
- (NSMutableArray *)dataList {
if (!_dataList) {
self.dataList = [NSMutableArray arrayWithCapacity:1];
for (int i = 0; i < 50; i++) {
[self.dataList addObject:[NSString stringWithFormat:@"%d - lanou", i]];
}
}
return [[_dataList retain] autorelease];
}
- (NSMutableArray *)searchList {
if (!_searchList) {
self.searchList = [NSMutableArray arrayWithCapacity:1];
}
return [[_searchList retain] autorelease];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// 设置负责更新搜索结果控制器内容的对象, 要遵守协议<UISearchResultsUpdating>
self.searchController.searchResultsUpdater = self;
// 在搜索的时候下面的内容是否变暗
self.searchController.dimsBackgroundDuringPresentation = NO;
// 在搜索的时候导航栏是否隐藏
self.searchController.hidesNavigationBarDuringPresentation = NO;
// 设置frame
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
// 将tableView 的页眉设置为searchBar
self.tableView.tableHeaderView = self.searchController.searchBar;
// self.searchController.searchBar.showsScopeBar = YES;
// NSArray *titles = @[@"1", @"2"];
// self.searchController.searchBar.scopeButtonTitles = titles;
// 设置背景图片
self.searchController.searchBar.backgroundImage = [UIImage imageNamed:@"banner"];
// 设置背景颜色
// 移除searchBarBackground
// [[self.searchController.searchBar.subviews objectAtIndex:0] removeFromSuperview];
// self.searchController.searchBar.backgroundColor = [UIColor redColor];
// 样式
// self.searchController.searchBar.barStyle = UIBarStyleDefault;
// self.searchController.searchBar.barStyle = UIBarStyleBlackOpaque;
[self.searchController release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
#pragma mark -- UISearchBarDelegate
// 编辑输入事件
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
return YES;
}// return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
}// called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return YES;
}// return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
}// called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
}// called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0) {
return YES;
}// called before text changes
// 点击按钮事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
}// called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
}// called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
}// called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) {
}// called when search results button pressed
// Scope 点击事件
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0) {
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
// 如果对搜索框有操作
if(self.searchController.active) {
return [self.searchList count];
}else {// 如果对搜索框没有操作
return [self.dataList count];
}
}
#pragma mark -- UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// 谓词 NSPredicate -- 它的操作是针对于数组类型的, 就好比数据库中的查询, 数据源就是数组, 好处是: 不需要写很多代码去操作数组, 同时也起到了过滤的作用. 我们可以编写简单的谓词语句, 就可以从数组中过滤出想要的数据.
NSString *searchStr = [self.searchController.searchBar text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchStr];
// SELF CONTAINS[c] %@, SELF表示要查询集合对象, CONTAINS[c]表示包含字符串, 其中c 不区分大小写.
if (self.searchList != nil) {
[self.searchList removeAllObjects];
}
// 过滤数据
self.searchList = [NSMutableArray arrayWithArray:[self.dataList filteredArrayUsingPredicate:predicate]];
// 刷新表格
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifer = @"reuse";
// 根据重用标志, 去tableView的重用队列中取可用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];
}
// cell显示的内容
if (self.searchController.active) {
cell.textLabel.text = self.searchList[indexPath.row];
}else {
cell.textLabel.text = self.dataList[indexPath.row];
}
// Configure the cell...
return cell;
}
@end