UISearchBar和UISearchDisplayController

UISearchBar和UISearchDisplayController实例应用

程序介绍:获取系统通讯录,利用 UISearchBar和UISearchDisplayController实现搜索功能

 1 #import <UIKit/UIKit.h>
 2
 3 @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
 4 {
 5     NSMutableArray *_dataArray;
 6
 7     //搜索结果的数组
 8     NSMutableArray *_resultArray;
 9
10     IBOutlet UITableView *_tableView;
11 }
12
13 @end

viewController.h

viewController.m

  1 #import "ViewController.h"
  2 #import <AddressBook/AddressBook.h>
  3
  4 @interface ViewController ()
  5
  6 @end
  7
  8 @implementation ViewController
  9
 10 - (void)viewDidLoad
 11 {
 12     [super viewDidLoad];
 13
 14     _dataArray = [[NSMutableArray alloc] init];
 15     _resultArray = [[NSMutableArray alloc] init];
 16
 17     //创建通讯录对象
 18     ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, NULL);
 19     //请求权限
 20     ABAddressBookRequestAccessWithCompletion(addressbook, ^(bool granted,CFErrorRef error){
 21
 22         //获取数据
 23         NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressbook);
 24         for (int i = 0; i < array.count; i++)
 25         {
 26             ABRecordRef person = array[i];
 27             NSString *name = (NSString *)ABRecordCopyCompositeName(person);
 28
 29             ABMultiValueRef ref = ABRecordCopyValue(person, kABPersonPhoneProperty);
 30             NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(ref, 0);
 31             NSLog(@"%@",phone);
 32
 33             NSString *newPhone = [self trimString:phone];
 34
 35
 36             NSDictionary *dic = @{@"name": name,@"phone":newPhone};
 37             //把字典添加到数组中
 38             [_dataArray addObject:dic];
 39
 40             CFRelease(name);
 41             CFRelease(ref);
 42             CFRelease(phone);
 43
 44         }
 45
 46         //1 YES  主线程
 47         //0 NO  分线程
 48         NSLog(@"%d",[NSThread isMainThread]);
 49         //一般刷新界面的操作要放到 主线程(UI线程中)
 50         //刷新表格  需要回到主线程中
 51
 52         //回到主线程的方法
 53         //1.回到主线程中要执行的方法
 54         //2.方法里面的参数
 55         //3.YES 同步   NO 异步
 56         [self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];
 57
 58
 59 //        [_tableView reloadData];
 60
 61     });
 62
 63
 64     //搜索条  UISearchBar
 65     UISearchBar *searchBar = [[UISearchBar alloc] init];
 66     searchBar.frame = CGRectMake(0, 20, 320, 44);
 67     //设置搜索条风格
 68     searchBar.searchBarStyle = UISearchBarStyleDefault;
 69     //设置搜索条占位符
 70     searchBar.placeholder = @"请输入内容";
 71     //设置搜索条背景颜色
 72 //    searchBar.barTintColor = [UIColor blueColor];
 73
 74     //是否显示书签按钮
 75     searchBar.showsBookmarkButton = YES;
 76     //是否显示取消按钮
 77     searchBar.showsCancelButton = YES;
 78     //是否显示结果集
 79     searchBar.showsSearchResultsButton = YES;
 80     //设置搜索条 搜索条件
 81     searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"按照姓名搜索",@"按照电话号码搜索", nil];
 82
 83     //设置代理
 84     searchBar.delegate = self;
 85
 86
 87     //把searchBar设置为表格的表头view
 88     _tableView.tableHeaderView = searchBar;
 89     [searchBar release];
 90 //    [self.view addSubview:searchBar];
 91
 92     //搜索结果的展示器   一般是和 UISearchBar 结合使用
 93     //集成 了一个searchBar和一个tableview (用来展示搜索结果)
 94     //1.结果展示器和哪个搜索条绑定
 95     //2.搜索到的内容要显示到哪个视图控制器上
 96     UISearchDisplayController *controller = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
 97     //设置结果展示器的代理
 98     controller.delegate = self;
 99     //设置结果展示器的数据源
100     controller.searchResultsDataSource = self;
101     controller.searchResultsDelegate = self;
102 }
103
104 //回到主线程
105 -(void)reloadTableView
106 {
107     NSLog(@"reload。。。%d",[NSThread isMainThread]);
108     [_tableView reloadData];
109 }
110
111 #pragma mark - searchBarDelegate
112 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
113 {
114     NSLog(@"搜索条开始编辑");
115 }
116
117 #pragma mark - searchDisplayDelegate
118 //搜索内容改变 并且刷新搜索结果集的表格
119 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
120 {
121     NSLog(@"搜索内容改变 %@",searchString);
122     //处理搜索内容
123
124     //清空上次搜索的内容
125     [_resultArray removeAllObjects];
126
127     //判断当前搜索的类型
128     if (controller.searchBar.selectedScopeButtonIndex == 0)//按照姓名搜索
129     {
130         for (NSDictionary *dic in _dataArray)
131         {
132             NSString *name = dic[@"name"];
133             //判断姓名中是否包含了 搜索字符串的内容
134             NSRange range = [name rangeOfString:searchString];
135             if (range.location != NSNotFound)
136             {
137                 //当前联系人姓名中包含了搜索的字符串
138                 //把匹配到的数据添加到搜索结果的数组中
139                 [_resultArray addObject:dic];
140
141             }
142         }
143     }
144     else//按照电话号码搜索
145     {
146         for (NSDictionary *dic in _dataArray)
147         {
148             NSString *phone = dic[@"phone"];
149             NSRange range = [phone rangeOfString:searchString];
150             if (range.location != NSNotFound)
151             {
152                 //匹配
153                 [_resultArray addObject:dic];
154             }
155         }
156     }
157
158     return YES;
159 }
160
161
162 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
163 {
164     NSLog(@"搜索条件切换");
165     [self  searchDisplayController:controller shouldReloadTableForSearchString:controller.searchBar.text];
166     return YES;
167 }
168
169
170 #pragma mark - dataSource
171 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
172 {
173     //判断当前执行协议方法的表格是哪一个
174     if (tableView == _tableView)
175     {
176         return _dataArray.count;
177     }
178     else//结果集表格
179     {
180         return _resultArray.count;
181     }
182
183 }
184
185 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
186 {
187     static NSString *cellID = @"Cell";
188     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
189     if (!cell)
190     {
191         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
192     }
193
194     //填写数据
195     NSDictionary *dic = nil;
196
197     if (tableView == _tableView)
198     {
199         dic = _dataArray[indexPath.row];
200     }
201     else
202     {
203         dic = _resultArray[indexPath.row];
204     }
205
206 //    NSDictionary *dic = _dataArray[indexPath.row];
207     cell.textLabel.text = dic[@"name"];
208     cell.detailTextLabel.text = dic[@"phone"];
209
210     return cell;
211 }
212
213
214 //过滤特殊字符
215 -(NSString *)trimString:(NSString *)string
216 {
217    string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
218     string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
219     string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
220     string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
221     return string;
222 }
223
224
225 -(void)dealloc
226 {
227     [_dataArray release];
228     [_resultArray release];
229     [super dealloc];
230 }
231
232 - (void)didReceiveMemoryWarning
233 {
234     [super didReceiveMemoryWarning];
235     // Dispose of any resources that can be recreated.
236 }
237
238 @end

VIewController.m

注:UISearchDisplayController搜索结果展示是以表的形式,所以包括DataSource和Delegate两个代理,数据代理与TableView一样.

时间: 2024-08-02 15:11:15

UISearchBar和UISearchDisplayController的相关文章

如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS

那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死板了,达不到想要的效果. 这里进行重新定制, 四个协议, 三个成员变量,第一步OK. @interface ViewController ()<UISearchBarDelegate,UISearchDisplayDelegate, UITableViewDataSource, UITableVie

IOS--常用控件--UISearchBar和UISearchDisplayController

一.UISearchBar单独使用时,设置样式: UIView *view =[mySearchBar.subviews objectAtIndex:0]; //    view.backgroundColor =[UIColor clearColor]; for (UIView *backview in view.subviews) { if ([backview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])

UISearchBar和 UISearchDisplayController的使用

上面是一个navigationController,接下来一个searchBar,下面是tableView searchBar这个控件就用来搜索tableView上的数据 [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; UISearchDisplayController这个控件很强大,它初始化是基于searchBar的,里面有些效果很不错,apple都封装好了,并且可

搜索栏UISearchBar和UISearchController(UISearchDisplayController在iOS8.0之后就不推荐使用)

iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜 索栏现在默认自家的神马搜索,现在不管是社交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是不一样的.iOS中的搜索栏实现起来相对 简单一点,网上也有很多参考资料,不过靠谱的不是很多,很多都是iOS 8.0之前的实现,iOS 8.0上的实现貌似很少看到,可以运行,不过会看到searchDisplayController'

UISearchDisplayController 使用

1.首先定义属性 @property (nonatomic, retain) UISearchDisplayController *searchDisplayController; 2.创建一个UISearchBar UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; [search setPlaceholder:@"搜索"

UISearchDisplayController搜索(iOS8前)

搜索功能,基本每个app标配. 实现起来很简单,但是iOS8后苹果建议使用UISearchController,官方Demo:Table Search with UISearchController 实际开发基本也都还是用的老的UISearchDisplayController+UISearchBar的方案,因为要照顾一些版本低的用户. 发现时间长没写都忘记差不多了,闲暇之余,一起整理下,方便以后翻阅. 这篇先从UISearchDisplayController开始.比较简单,就不罗嗦了,直接贴

iOS UISearchDisplayController学习笔记

UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示.UISearchDisplayController提供了显示搜索结果的tableview来覆盖原控制器的视图: 使用UISearchDisplayController需要: 提供搜索结果table的数据的来源-searchResultsDataSource 搜索结果table的代理 SearchResultsDelegate UISearchDisplayContro

iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是不一样的.iOS中的搜索栏实现起来相对简单一点,网上也有很多参考资料,不过靠谱的不是很多,很多都是iOS 8.0之前的实现,iOS 8.0上的实现貌似很少看到,可以运行,不过会看到searchDisplayController' is

自己总结的 iOS ,Mac 开源项目以及库,知识点------持续更新

自己在 git  上看到一个非常好的总结的东西,但是呢, fork  了几次,就是 fork  不到我的 git 上,干脆复制进去,但是,也是认真去每一个每一个去认真看了,并且也是补充了一些,感觉非常棒,所以好东西要分享,为啥用 CN 博客,有个好处,可以随时修改,可以持续更新,不用每次都要再发表,感觉这样棒棒的 我们 自己总结的iOS.mac开源项目及库,持续更新.... github排名 https://github.com/trending,github搜索:https://github.