Imitating the iphone address book search function.

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.