SearchBar and Search Display Controller?

Discussion in 'iOS Programming' started by Icarox, Oct 15, 2012.

  1. Icarox, Oct 15, 2012
    Last edited: Oct 15, 2012

    Icarox macrumors newbie

    Joined:
    Oct 13, 2012
    #1
    Hi, I'm trying to add a asearch bar on a tableView.

    I added the code but it does nothing :confused:

    Here's what it is in the h:

    Code:
    @interface FlipsideViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, AddFormControllerDelegate,SVGeocoderDelegate, UISearchBarDelegate> {
        //UISearchDisplayController *searchDisplayController;
        UISearchBar *searchBar;
        NSArray *allIems;
        NSArray *searchResults;
        DetailViewController *detailViewController;
        UITableView *informationsTableView;
        UIToolbar *toolbar;
        UIImageView *background;
        UISegmentedControl *segmentedControl;
        MKMapView *mapView;
        CLLocationManager *locationManager;
        UIBarButtonItem *locateUserButton;
        NSMutableArray *annotations;
        NSArray *paths;
        NSArray *sortedNames;
        NSString *myPathDocs;
        NSString *documentsDirectory;
        // THIS HAS BEEN CHANGED ON VERSION 2.0.
        NSString *userLatitude;
        NSString *userLongitude;
        // END OF WHAT HAS BEEN CHANGED ON VERSION 2.0.
        BOOL goToUserLocation;
        BOOL firstLoad;
        BOOL result;
    
    
    }
    @property (retain, nonatomic) IBOutlet UISearchDisplayController *searchDisplayController;
    @property (retain, nonatomic) IBOutlet UISearchBar *searchBar;
    
    
    @property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
    @property (nonatomic, retain) IBOutlet UITableView *informationsTableView;
    @property (nonatomic, retain) IBOutlet MKMapView *mapView;
    @property (nonatomic, retain) IBOutlet UIImageView *background;
    @property (nonatomic, retain) IBOutlet UIBarButtonItem *locateUserButton;
    @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
    @property (nonatomic, copy) NSArray *allitems;
    @property (nonatomic, copy) NSArray *searchResults;
    
    
    
    
    - (IBAction)done:(id)sender;
    - (IBAction)locateUser:(id)sender;
    - (IBAction)locateHome:(id)sender;
    - (IBAction)setMapType:(id)sender;
    
    - (void)onSegmentedControlChanged:(UISegmentedControl *)sender;
    - (void)loadAnnotations;
    - (void)AddFormControllerDidFinish:(AddFormViewController *)controller;
    - (void) showContactForm;
    
    
    
    @end
    And here what's in the m:

    Code:
    - (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control { 
        // User pressed the blue button on the right of a callout on the map.
        // Muy importante poner el mismo orden en *objects que en *keys. 
        firstLoad = NO;
        
        MyAnnotations *theAnnotation = (MyAnnotations *)pin.annotation;
        
        DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        NSArray *objects=[NSArray arrayWithObjects:
                          theAnnotation.title,
                          theAnnotation.subtitle,
                          theAnnotation.address,
                          theAnnotation.rating,
                          theAnnotation.AvPrice,
                          theAnnotation.description,
                          theAnnotation.comments,
                          theAnnotation.category,
                          nil];
        NSArray *keys=[NSArray arrayWithObjects:
                       @"title",
                       @"subtitle",
                       @"address",
                       @"rating",
                       @"AvPrice",
                       @"description",
                       @"comments",
                       @"category",
                       nil];
        
        
        controller.CrewpointsData=[NSDictionary dictionaryWithObjects:objects forKeys:keys];    
        controller.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
        controller.navigationItem.title = theAnnotation.title;
        self.allitems = keys;
        [keys release];
        
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
    }
    
    #pragma mark -
    #pragma mark UITableView Data Source
    
    // Here we're going to take care of the List view.
    
    // Set TableView Number of Sections
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	// Return the number of Table View Sections
        return 1;
    }
    
    //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    //    return @"Puntos de interés de Tripulantes";
    //}
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [sortedNames count];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 65.0;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"CustomTableCell";
        static NSString *CellNib = @"DetailViewCell";
        
        NSDictionary *name = [sortedNames objectAtIndex:indexPath.row];
        
        DetailViewCell *cell = (DetailViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
            cell = (DetailViewCell *)[nib objectAtIndex:0];
        }
        
        // Delete current cellTitleLabel, cellSubtitleLabel & cellcategoryLabel because we're reusing the cells.
        cell.cellTitleLabel.text = @"";
        cell.cellSubtitleLabel.text = @"";
        cell.cellcategoryLabel.text = @"";
        cell.cellLocalidadLabel.text = @"";
        
        cell.cellTitleLabel.text = [name objectForKey:@"title"];
        cell.cellSubtitleLabel.text = [name objectForKey:@"subtitle"];
        cell.cellcategoryLabel.text = [name objectForKey:@"category"];
        cell.cellLocalidadLabel.text = [name objectForKey:@"localidad"];
    
        
        return (DetailViewCell *) cell;	
    }
    
    - (void)filterContentForSearchText: (NSString*)searchText
                                 scope: (NSString*)scope{
        NSPredicate *resultPredicate =[NSPredicate predicateWithFormat:@"SELF contains[cd] %", searchText];
        self.searchResults = [self.allitems filteredArrayUsingPredicate:resultPredicate];
    }
    
    #pragma mark - UISearchDisplayController delegate methods
    - (BOOL)seachDisplayController:(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;
    }
    
    
    
    
    
    #pragma mark -
    #pragma mark UITableView Delegate
    
    // Called just before a new table row is selected
    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
        return indexPath;
    }
    
    // Called after the user changes the selection.
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        firstLoad = NO;
        
    	// Remove the row selection highlight
    	[tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        
        controller.CrewpointsData=[sortedNames objectAtIndex:indexPath.row];
        
        controller.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
        
        controller.navigationItem.title = [controller.CrewpointsData objectForKey:@"title"];
        
        [self.navigationController pushViewController:controller animated:YES];
        
        [controller release];
    }
    
    Here are the connections, first one for searchbar and the other bottom one fro SearchDisplayController:

    [​IMG]

    I'd like it to search by the object Subtitle.

    Thanks in advance.:)
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Care to elaborate? This statement is too vague for us to go on. What are you expecting to see / what do you actually see? Any compile-time or run-time errors? Etc.
     
  3. Icarox thread starter macrumors newbie

    Joined:
    Oct 13, 2012
    #3
    Hi,

    When I try to search on the viewTable the search bar is present... all is connected...

    [​IMG]
    [​IMG]

    But searching for some word starting with "b", for example, shows the whole list and not places starting with "b".

    [​IMG]

    Rgds.
     
  4. caffeineios macrumors newbie

    Joined:
    Oct 9, 2012
    #4
    I think the problem is with your filterContentForSearchText:scope:

    The search tableview uses your main tableview's datasource and delegate (via the searchDisplayController). So what you want to do is keep one array with all your results (sortedNames) and have one filtered array (searchResults). Initially you can set searchResults to be a copy of sortedNames, and use searchResults everywhere instead of sortedNames (in tableView:numberOfRowsInSection: etc).

    Then in your filterContentForSearchText:scope: you can update your searchResults like so:

    Code:
    NSPredicate *resultPredicate =[NSPredicate predicateWithFormat:@"subtitle contains[cd] %@", searchText];
    self.searchResults = [self.sortedNames filteredArrayUsingPredicate:resultPredicate];
    Also make sure to set searchResults back to a copy of sortedNames when the search is ended/cancelled.
     
  5. Icarox thread starter macrumors newbie

    Joined:
    Oct 13, 2012
    #5
    Hi caffeineios,

    I set the code to your suggestion but nothing still happens. I t just search for nothing :(

    Thanks
     

Share This Page