0

Eine Suchansicht erstellen, das ist der Code, den ich für die Hauptsuchansicht gemacht habe.Wie man UISearchBar einmal absetzen kann

Was passiert, wenn ich nicht suche/filtere, wird die uisearchbar entlassen, wenn segueing. aber wenn ich suche/filtere, dann bleibt die uisearchbar auf der nav-bar, wenn man sie ansteuert.

- (void)viewDidLoad { 
     [super viewDidLoad]; 

     // There's no transition in our storyboard to our search results tableview or navigation controller 
     // so we'll have to grab it using the instantiateViewControllerWithIdentifier: method 
     UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"CompanySearchResultsNavigationController"]; 

     // Our instance of UISearchController will use searchResults 
     self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 

     // The searchcontroller's searchResultsUpdater property will contain our tableView. 
     self.searchController.searchResultsUpdater = self; 

     self.searchController.hidesNavigationBarDuringPresentation = NO; 


     // The searchBar contained in XCode's storyboard is a leftover from UISearchDisplayController. 
     // Don't use this. Instead, we'll create the searchBar programatically. 
     self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0); 



     self.navigationItem.titleView = self.searchController.searchBar; 


     self.definesPresentationContext = YES; 


    } 

    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
    } 

    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     return [self.objects count]; 
    } 



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 
     CompanySearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchCell" forIndexPath:indexPath]; 

     cell.productImageView.file = (PFFile *)object[@"profileImage"]; 
     cell.productImageView.layer.cornerRadius = cell.productImageView.frame.size.width/2; 
     cell.productImageView.clipsToBounds = YES; 
     [cell.productImageView loadInBackground]; 

     cell.companyNameLabel.text = object[@"username"]; 

     return cell; 
    } 

    #pragma mark - UISearchControllerDelegate & UISearchResultsDelegate 

    // Called when the search bar becomes first responder 
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController 
    { 

     // Set searchString equal to what's typed into the searchbar 
     NSString *searchString = self.searchController.searchBar.text; 


      [self updateFilteredContentForAirlineName:searchString]; 

     // If searchResultsController 
     if (self.searchController.searchResultsController) { 

      UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController; 

      // Present SearchResultsTableViewController as the topViewController 
      CompanySearchResultsTableViewController *vc = (CompanySearchResultsTableViewController *)navController.topViewController; 

      // Update searchResults 
      vc.searchResults = self.searchResults; 

      // And reload the tableView with the new data 
      [vc.tableView reloadData]; 
     } 
    } 


    // Update self.searchResults based on searchString, which is the argument in passed to this method 
    - (void)updateFilteredContentForAirlineName:(NSString *)companyName 
    { 

     if (companyName == nil) { 

      // If empty the search results are the same as the original data 
      self.searchResults = [self.objects mutableCopy]; 
     } else { 

      NSMutableArray *searchResults = [[NSMutableArray alloc] init]; 

      // Else if the airline's name is 
      for (PFObject *company in self.objects) { 
       if ([company[@"username"] containsString:companyName]) { 

    //    NSString *str = [NSString stringWithFormat:@"%@", company[@"username"]]; 
    //    [searchResults addObject:str]; 

        PFObject *searchedObject = company; 
        [searchResults addObject:searchedObject]; 

        NSLog(@"Searched: %@",searchedObject[@"username"]); 
       } 

       self.searchResults = searchResults; 

      } 
     } 
    } 
+0

Ich gehe davon aus, was Sie wollen zu einem anderen Teil Ihrer App Segue ist, richtig? Zum Beispiel, wenn der Benutzer ein Unternehmen auswählt, sollte die App auf einen Bildschirm gehen, der einige Details über dieses Unternehmen zeigt. – moonman239

+0

ja genau, du hast es. – farhan

+0

In diesem Fall sehe ich keinen Grund, warum mein Code nicht funktioniert. – moonman239

Antwort

0

In Ihrem AirlineTableViewController, außer Kraft setzen [UIViewController prepareForSegue]:

- (void)prepareForSegue:(UIStoryboardSegue *)segue 
       sender:(id)sender { 
[self.searchController setActive:NO]; 
} 
+0

So habe ich zwei TableViewControllers, der anfängliche Tableview-Controller entlässt fein. es ist nur, dass, wenn ich etwas tue und suche etwas das Ergebnis tableviewcontroller ist dann ein Top-View-Controller, so dass ich glaube nicht, dass dies funktionieren würde. – farhan

+0

das ist, was ich folgte http://www.jhof.me/simple-uisearchcontroller-implementation/ – farhan