标签:
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.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
注:UISearchDisplayController搜索结果展示是以表的形式,所以包括DataSource和Delegate两个代理,数据代理与TableView一样.
UISearchBar和UISearchDisplayController
标签:
原文地址:http://www.cnblogs.com/kyuubee/p/4805655.html