0

So I am using the UISearchController in my project, and it seems to work all fine but whatever I type on the Search Bar no I get no results. So I believe the problem is on my (void)updateSearchResultsForSearchController:(UISearchController *)searchController { }. To be honest, I don't really know what code to put in that method. Here is the rest of the code:

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) SearchResultsTableViewController *resultsTableController;
@property (nonatomic, strong) NSMutableArray *searchResults; // Filtered search results
// for state restoration
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;

 _resultsTableController = [[SearchResultsTableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;

self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; 
self.searchController.searchBar.delegate = self; 
self.searchController.searchBar.tintColor = [UIColor darkGrayColor];
self.definesPresentationContext = YES;  

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

// restore the searchController's active state
if (self.searchControllerWasActive) {
    self.searchController.active = self.searchControllerWasActive;
    _searchControllerWasActive = NO;

    if (self.searchControllerSearchFieldWasFirstResponder) {
        [self.searchController.searchBar becomeFirstResponder];
        _searchControllerSearchFieldWasFirstResponder = NO;
    }
}}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];}


#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
4

1 回答 1

0

您需要获取表视图数据来自的 NSArray,根据搜索条件对其进行过滤,然后重新加载表视图。您的代码可能如下所示:

- (void) doSearch:(NSString *)searchText {
    [self.filteredClients removeAllObjects];

    if ( [searchText length] == 0 ) {
        [self.filteredClients addObjectsFromArray:self.users];
    } else {
        for (User *user in self.users) {
            NSString *name = [user fullName];
            NSRange range = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];
            if ( range.location != NSNotFound )
                [self.filteredClients addObject:user];
        }
    }
    [self.tableView reloadData];
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ( [searchText length] > 0 )
    {
        [self showCancelButton:YES];
    }
    else {
        [self showCancelButton:NO];
        [searchBar resignFirstResponder];
    }
    [self doSearch:searchText];
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [self showCancelButton:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    if ( [searchBar.text length] > 0 )  [self showCancelButton:YES];
    else                                [self showCancelButton:NO];
    [self doSearch:_searchBar.text];
}
于 2014-12-04T03:00:44.270 回答