iOS 用UISearchDisplayController实现查找功能

  UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用

  示例代码如下:

 //
// WKRootViewController.m
// 表格视图的搜索功能
//
// Created by student on 14-10-20.
// Copyright (c) 2014年 wukong. All rights reserved.
// #import "WKRootViewController.h" @interface WKRootViewController () @property (strong, nonatomic) NSMutableArray* dataSource; @property (strong, nonatomic)NSMutableArray* resultArrat; @end @implementation WKRootViewController
{
//用于加载数据源的表视图
UITableView *_tableView; UISearchBar *_searchBar; UISearchDisplayController *_searchControl;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; [self createUI];
[self createDataSource];
// Do any additional setup after loading the view.
} - (void)createDataSource
{
_dataSource = [[NSMutableArray alloc] init];
_resultArrat = [[NSMutableArray alloc] init];
for (int i = 'A'; i <= 'z'; i++) {
NSMutableArray *section = [[NSMutableArray alloc] init];
for (int j = ; j <= ; j++) {
NSString *str = [NSString stringWithFormat:@"%c-%d", i, j];
[section addObject:str];
}
[_dataSource addObject:section];
}
} #pragma mark- UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//判断当前展示的表格
if (tableView != _tableView)
return ;
return _dataSource.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView != _tableView) {
return _resultArrat.count;
}
return [[_dataSource objectAtIndex:section] count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (tableView != _tableView) {
cell.textLabel.text = [_resultArrat objectAtIndex:indexPath.row];
}else{
cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
return cell;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - CreateUI
- (void)createUI
{
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , , )];
_searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchBar.delegate = self;
[_tableView setTableHeaderView:_searchBar];
/*
第一个参数:用于输入搜索内容的UISearchBar对象
第二个参数:提供给我的表格视图数据源的控制器对象,这个对象必须是实现了表格的两个协议
*/
_searchControl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
// _searchControl.searchResultsTableView
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
// label.backgroundColor =[UIColor redColor];
// [_searchControl.searchResultsTableView setTableHeaderView:label];
//设置_searchControl自带的表格视图的委托对象
[_searchControl setSearchResultsDataSource:self];
[_searchControl setSearchResultsDelegate:self];
} #pragma mark -UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[_resultArrat removeAllObjects];
NSString *str = [NSString stringWithFormat:@"*%@*", searchText];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", str];
for (NSMutableArray *arr in _dataSource) {
for (NSString *str in arr) {
if ([pred evaluateWithObject:str]) {
[_resultArrat addObject:str];
}
}
}
}
@end
上一篇:CentOS-6.5安装配置JDK-7|Tomcat-8


下一篇:CentOS-6.5安装配置JDK-7