Imitating the iphone address book
The project has previously implemented the pinyin sorting Address Book Design. Now you can search for the address book.
The original interface is as follows:
The search function depends on uisearchbar uisearchdisplaycontroller.
Step 1 initialization: Initialize searbar and seardisplaycontroller, and the corresponding two arrays (used to display all results and search results) and corresponding delegate
@interface ComunicationViewController ()
{ UISearchDisplayController *searchDisplayController; UISearchBar *searchBar; NSArray *allItems; NSArray *searchResults;}
SearchBar = [[UISearchBar alloc] init]; searchBar. delegate = self; searchBar. placeholder = @ "enter your name"; [searchBar setAutocapitalizationType: UITextAutocapitalizationTypeNone]; [searchBar sizeToFit]; comunicationTabelView. tableHeaderView = searchBar; // comunicationTabelView. frame = CGRectMake (0, 0,320, comunicationTabelView. frame. size. height); searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self]; [searchDisplayController setDelegate: self]; [searchDisplayController setSearchResultsDataSource: self];
Step 2: A matching method and two delegate
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; searchResults = [[allItems valueForKey:@"RealName"] filteredArrayUsingPredicate:resultPredicate]; [searchResults retain]; NSLog(@"%@",searchResults); }#pragma mark - UISearchDisplayController delegate methods-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; }- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; return YES; }
The most important thing is tableview judgment during loading.
# Pragma mark-comunicationTableView the number of rows in the proxy method // section-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {if (tableView = searchDisplayController. searchResultsTableView) {return searchResults. count;} else {return [[numArr objectAtIndex: section] intValue] ;}// height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 50;} // number of sections-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {if (tableView = searchDisplayController. searchResultsTableView) {return 1;} else {return ziMuArr. count ;}// section name-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {if (tableView = searchDisplayController. searchResultsTableView) {return nil;} else {return [ziMuArr objectAtIndex: section] ;}- (NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {if (tableView = searchDisplayController. searchResultsTableView) {return nil;} else {return ziMuArr ;}}
Finally, we achieved the following: