0

検索ビューを作成すると、これはメイン検索ビューで行ったコードです。一度セグメンテーションされたUISearchBarを解除する方法

検索/フィルタを使用しないと、セグメンテーション時にuisearchbarが終了します。しかし、もし私が検索/フィルタリングすると、uisearchbarはseguing時にnav barにとどまります。あなたのAirlineTableViewControllerで

- (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

私はあなたが望むものと仮定しています正しい、あなたのアプリケーションの別の部分にセグエするのですか?たとえば、ユーザーが会社を選択すると、その会社の詳細を示す画面が表示されます。 – moonman239

+0

はい、あなたはそれを持っています。 – farhan

+0

その場合、私のコードがうまくいかない理由はありません。 – moonman239

答えて

0

、[のUIViewController prepareForSegue]をオーバーライドします。

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

私は2つのtableViewControllerを持っているので、初期のtableviewコントローラは正常に終了します。そのちょうど私が入力して検索結果をtableviewcontrollerは、トップviewcontrollerされているので、私はこれが動作するとは思わない。 – farhan

+0

これは私がhttp://www.jhof.me/simple-uisearchcontroller-implementation/に従ったものです – farhan

関連する問題