手機通訊錄

搜索功能主要依靠 uisearchbar uisearchdisplaycontroller來實現

第一步  初始化:初始化searbar 和seardisplaycontroller 以及 對應的兩個數組(用來顯示全部結果以及搜索結果)還有對應的delegate

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. @interface ComunicationViewController ()<UISearchBarDelegate,UISearchDisplayDelegate>  
  2.   
  3. {  
  4.     UISearchDisplayController *searchDisplayController;  
  5.     UISearchBar *searchBar;  
  6.     NSArray *allItems;  
  7.     NSArray *searchResults;  
  8.   
  9. }  


[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. searchBar = [[UISearchBar alloc] init];  
  2. searchBar.delegate = self;  
  3. searchBar.placeholder = @"請輸入姓名";  
  4. [searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];  
  5.   
  6. [searchBar sizeToFit];  
  7.   
  8. comunicationTabelView.tableHeaderView = searchBar;  
  9. //comunicationTabelView.frame = CGRectMake(0, 0, 320, comunicationTabelView.frame.size.height);  
  10.   
  11.   
  12.   
  13. searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];  
  14.   
  15. [searchDisplayController setDelegate:self];  
  16.   
  17. [searchDisplayController setSearchResultsDataSource:self];  
  18.   
  19. [searchDisplayController setSearchResultsDelegate:self];  
第二步 一個匹配的方法 和2個delegate

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {  
  2.       
  3.     NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",                                     searchText];  
  4.       
  5.     searchResults = [[allItems valueForKey:@"RealName"] filteredArrayUsingPredicate:resultPredicate];  
  6.     [searchResults retain];  
  7.     NSLog(@"%@",searchResults);  
  8.       
  9. }  
  10.   
  11.   
  12.   
  13.   
  14. #pragma mark - UISearchDisplayController delegate methods  
  15.   
  16. -(BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchString:(NSString *)searchString {  
  17.       
  18.     [self filterContentForSearchText:searchString  scope:[[self.searchDisplayController.searchBar scopeButtonTitles objectAtIndex:[self.searchDisplayController.searchBar                                                      selectedScopeButtonIndex]]];  
  19.       
  20.     return YES;  
  21.       
  22. }  
  23.   
  24. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchScope:(NSInteger)searchOption {  
  25.       
  26.     [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];  
  27.       
  28.     return YES;  
  29.       
  30. }  
最重要的是加載時:tableview判斷

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #pragma mark -comunicationTableView代理方法  
  2. //section中的row數  
  3. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  4. {  
  5.     if (tableView == searchDisplayController.searchResultsTableView)  
  6.     {  
  7.         return searchResults.count;  
  8.     }  
  9.     else  
  10.     {  
  11.         return[[numArr objectAtIndex:section] intValue] ;  
  12.     }  
  13. }  
  14. //高度  
  15. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  16. {  
  17.     return 50;  
  18. }  
  19. //section數  
  20. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  21. {  
  22.     if (tableView == searchDisplayController.searchResultsTableView)  
  23.     {  
  24.         return 1;  
  25.     }  
  26.     else  
  27.     {  
  28.         return ziMuArr.count;  
  29.     }  
  30. }  
  31. //section 的名字  
  32. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  33. {  
  34.     if (tableView == searchDisplayController.searchResultsTableView)  
  35.     {  
  36.         return nil;  
  37.     }  
  38.     else  
  39.     {  
  40.         return [ziMuArr objectAtIndex:section];  
  41.     }  
  42. }  
  43.   
  44. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView  
  45. {    if (tableView == searchDisplayController.searchResultsTableView)  
  46. {  
  47.     return nil;  
  48. }  
  49. else  
  50. {  
  51.     return ziMuArr;  
  52. }  
  53. }  
發佈了12 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章