UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用
示例代码如下:
1
//
2
//
WKRootViewController.m
3
//
表格视图的搜索功能
4
//
5
//
Created by student on 14-10-20.
6
//
Copyright (c) 2014年 wukong. All rights reserved.
7
//
8
9
#import
"
WKRootViewController.h
"
10
11
@interface
WKRootViewController ()
12
13 @property (strong, nonatomic) NSMutableArray* dataSource;
14 15 @property (strong, nonatomic)NSMutableArray* resultArrat;
16 17 18@end 19 20@implementation WKRootViewController
21{
22//用于加载数据源的表视图 23 UITableView *_tableView;
24 25 UISearchBar *_searchBar;
26 27 UISearchDisplayController *_searchControl;
28}
29 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
30{
31 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
32if (self) {
33// Custom initialization 34 }
35return self;
36}
37 38 - (void)viewDidLoad
39{
40 [super viewDidLoad];
41 42 [self createUI];
43 [self createDataSource];
44// Do any additional setup after loading the view. 45}
46 47 - (void)createDataSource
48{
49 _dataSource = [[NSMutableArray alloc] init];
50 _resultArrat = [[NSMutableArray alloc] init];
51for (int i = ‘A‘; i <= ‘z‘; i++) {
52 NSMutableArray *section = [[NSMutableArray alloc] init];
53for (int j = 1; j <= 10; j++) {
54 NSString *str = [NSString stringWithFormat:@"%c-%d", i, j];
55 [section addObject:str];
56 }
57 [_dataSource addObject:section];
58 }
59}
60 61#pragma mark- UITableViewDataSource
62 63 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
64{
65//判断当前展示的表格 66if (tableView != _tableView)
67return1;
68return _dataSource.count;
69}
70 71 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
72{
73if (tableView != _tableView) {
74return _resultArrat.count;
75 }
76return [[_dataSource objectAtIndex:section] count];
77}
78 79 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
80{
81 [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
82 83 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
84if (tableView != _tableView) {
85 cell.textLabel.text = [_resultArrat objectAtIndex:indexPath.row];
86 }else{
87 cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
88 }
89return cell;
90}
91 92 - (void)didReceiveMemoryWarning
93{
94 [super didReceiveMemoryWarning];
95// Dispose of any resources that can be recreated. 96}
97 98 99#pragma mark - CreateUI
100 - (void)createUI
101{
102 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 30, 300, 440) style:UITableViewStylePlain];
103 _tableView.delegate = self;
104 _tableView.dataSource = self;
105 [self.view addSubview:_tableView];
106107 _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
108 _searchBar.searchBarStyle = UISearchBarStyleMinimal;
109 _searchBar.delegate = self;
110 [_tableView setTableHeaderView:_searchBar];
111/*112 第一个参数:用于输入搜索内容的UISearchBar对象
113 第二个参数:提供给我的表格视图数据源的控制器对象,这个对象必须是实现了表格的两个协议
114*/115 _searchControl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
116// _searchControl.searchResultsTableView
117// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
118// label.backgroundColor =[UIColor redColor];
119// [_searchControl.searchResultsTableView setTableHeaderView:label];
120//设置_searchControl自带的表格视图的委托对象121 [_searchControl setSearchResultsDataSource:self];
122 [_searchControl setSearchResultsDelegate:self];
123}
124125#pragma mark -UISearchBarDelegate
126 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
127{
128 [_resultArrat removeAllObjects];
129 NSString *str = [NSString stringWithFormat:@"*%@*", searchText];
130 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", str];
131for (NSMutableArray *arr in _dataSource) {
132for (NSString *str in arr) {
133if ([pred evaluateWithObject:str]) {
134 [_resultArrat addObject:str];
135 }
136 }
137 }
138}
139@end
原文:http://www.cnblogs.com/pretty-guy/p/4063539.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!