UISearchController 结果未被过滤
Posted
技术标签:
【中文标题】UISearchController 结果未被过滤【英文标题】:UISearchController results are not being filtered 【发布时间】:2014-12-04 02:27:44 【问题描述】:所以我在我的项目中使用 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】:您需要获取表视图数据来自的 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];
【讨论】:
嘿,拉里,我不确定你在 for 条件下所说的用户是什么意思?? NSString *searchText = searchController.searchBar.text; NSMutableArray *searchResults = [self.tableData mutableCopy]; if ( [searchText 长度] == 0 ) [self.searchResults addObjectsFromArray:self.tableData]; else for (NSString *searchString in self.tableData) NSString *name = [searchString self]; NSRange range = [[name lowercaseString] rangeOfString:[searchText lowercaseString]]; if ( range.location != NSNotFound ) [self.searchResults addObject:searchString]; [self.tableView reloadData]; 对不起塞尔吉奥。我应该解释一下,我的初始数据来自一个用户的 NSArray(这里是 self.users),每个用户都是一个 User 类(基本上是一个 NSDictionary)。我遍历每个用户,将他们的全名与搜索文本进行比较。如果找到,我将该条目添加到 self.filtered clients NSArray,用于填充我的表格视图。然后我重新加载表格视图。您会注意到,如果没有搜索文本,我将所有用户都添加到 self.filteredclients,以便表格视图将它们全部显示出来。 那么创建一个类是否必要?我的意思是在我的项目中,我刚刚创建了一个 NSArray,这将是我的 self.tableData,与您的 NSArray 用户相同。 没有。我的代码有一个类,因为它使用了其他东西。我为您创建了一个要点,可以使用 NSArray 和 NSDictionary - gist.github.com/lborsato/d52ca5e286111f84fcf5.以上是关于UISearchController 结果未被过滤的主要内容,如果未能解决你的问题,请参考以下文章
UISearchController 或服务请求结果的其他选项?
使用 UISearchController 时如何删除排序/过滤的项目
在 UISearchController 下添加水平滚动过滤器选项