UISearchController结果未被过滤

时间:2014-12-04 02:27:45

标签: objective-c ios8 uisearchbar uisearchcontroller

所以我在我的项目中使用了UISearchController,它似乎工作得很好,但无论我在搜索栏上输入什么,我都没有得到任何结果。所以我相信问题出在我的身上  (void)updateSearchResultsForSearchController:(UISearchController *)searchController { } 即可。说实话,我真的不知道在该方法中放入什么代码。以下是代码的其余部分:

@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 {
}

1 个答案:

答案 0 :(得分: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];
}