我有一个UISearchDisplayController
用于显示一长串选项供用户选择的选项。我使用控制器提供的搜索栏过滤列表。
一切正常,除了在搜索栏中输入文本外,我无法显示结果表。我想让表格显示我拥有的所有结果,即使搜索栏中没有文本。目前,当没有文字时,我只有一个暗淡的底层视图。
请问这可能吗?或者我应该根本不打扰UISearchDisplayController
,只在我显示的另一个 tableview 中有一个搜索栏,然后将它从堆栈中弹出?
我有一个UISearchDisplayController
用于显示一长串选项供用户选择的选项。我使用控制器提供的搜索栏过滤列表。
一切正常,除了在搜索栏中输入文本外,我无法显示结果表。我想让表格显示我拥有的所有结果,即使搜索栏中没有文本。目前,当没有文字时,我只有一个暗淡的底层视图。
请问这可能吗?或者我应该根本不打扰UISearchDisplayController
,只在我显示的另一个 tableview 中有一个搜索栏,然后将它从堆栈中弹出?
要在键盘出现时使用空的 searchBar 立即显示搜索结果,请将以下代码添加到 ViewController:
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[controller.searchBar.delegate searchBar:controller.searchBar textDidChange:@" "];
}
要在用户输入一些文本然后清除搜索框时保持表格显示结果,请添加:
-(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
[controller.searchBar.delegate searchBar:controller.searchBar textDidChange:@" "];
}
不要忘记在searchDisplayController:shouldReloadTableForSearchString 中返回 YES:
通常,您希望首先将所有内容加载到单个数组中,然后将表格视图显示为“正常”。
一旦这工作,创建第二个数组,其中包含搜索结果
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
然后,在表视图的委托/数据源方法中,通过检查来决定您所在的上下文
if (_tableView == self.searchDisplayController.searchResultsTableView)