Programmatic search in iOS

Code Sample:

@interface MyViewController

@property (strong, nonatomic) NSArray *venues;
@property (strong, nonatomic) NSArray *searchResults;

@end

- (void)loadView
{
   [super loadView];

       UISearchBar *searchBar = [[UISearchBar alloc]init];
    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    self.searchController.searchResultsDelegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.delegate = self;
    self.tableView.tableHeaderView = searchBar;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return self.searchResults.count;

    } else {
        return self.venues.count;
    }
}

// Helper function that retrieves a data source for the specified table view
- (NSArray *) dataSourceForTableView:(UITableView *)tableView
{
    if(tableView == self.searchDisplayController.searchResultsTableView) {
        return self.searchResults;
    }
    else {
        return self.venues;
    }
}

// Helper function that retrieves a value for a tableview and index path
- (Venue *)venueForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    NSArray *dataSource = [self dataSourceForTableView:tableView];
    return (Venue *)dataSource[indexPath.row];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            NSString *identifier = @"choose_venue_cell";
            VenueCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

            // construct a new cell
            if(cell == nil)
            {
                cell = [[VenueCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }


            Venue *venue = [self venueForTableView:tableView indexPath:indexPath];
            CLLocation *location = [LocationService sharedInstance].currentLocation;
            [cell setVenue:venue userLocation:location];

            return cell;
        }
        case 1:
        {
            NSString *identifier = @"foursquare_attribution";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                cell.backgroundColor = [UIColor clearColor];

                FoursquareAttribution *foursquareAttribution = [[FoursquareAttribution alloc]initWithFrame:cell.frame];
                [cell addSubview:foursquareAttribution];
            }
            return cell;
        }
        default:
            return nil;
    }
}

#pragma mark - Search display delegate

//-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
//{
//    NSLog(@"Search results table view loaded");
//    NSLog(@"Rows in section 1: %lu", (long)[tableView.dataSource tableView:tableView numberOfRowsInSection:0]);
//}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    return false;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self loadVenuesForTable:self.searchController.searchResultsTableView withSearch:searchString];
    return false;
}

-(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"willShowSearchResultsTableView");
}

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"didShowSearchResultsTableView");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"willHideSearchResultsTableView");
}

-(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"didHideSearchResultsTableView");
}



#pragma mark - Search bar delegate

//-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
//{
////    NSLog(@"Search text is now: %@", searchText);
//}

//-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
//{
//    NSString *searchString = self.searchController.searchBar.text;
//    NSLog(@"Searching for %@", searchString);
//    [self loadVenuesForTable:self.searchController.searchResultsTableView withSearch:searchString];
//}

References:
UISearchDisplayDelegate, UISearchBarDelegate

https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchDisplayControllerClass/Reference/Reference.html#//appleref/doc/uid/TP40008225-CH1-SW9

https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchDisplayDelegateProtocol/Reference/Reference.html#//appleref/occ/intf/UISearchDisplayDelegate

https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarClass/Reference.html#//appleref/doc/uid/TP40007529

https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarDelegateProtocol/Reference/Reference.html#//appleref/occ/intf/UISearchBarDelegate

http://stackoverflow.com/questions/5920963/howto-implement-uisearchdisplaycontroller-in-a-uiviewcontroller-without-interfac

comments powered by Disqus