一旦被诱惑,如何解雇UISearchBar

时间:2016-06-16 20:56:51

标签: ios objective-c uitableview uinavigationcontroller uisearchbar

创建搜索视图,这是我为主搜索视图所做的代码。

如果我不搜索/过滤会发生什么,uisearchbar会在segueing时被解雇。但如果我搜索/过滤,那么uisearchbar会在搜索时保留在导航栏上。

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

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

在你的AirlineTableViewController中,覆盖[UIViewController prepareForSegue]:

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