我有一个UITableView,顶部有一个搜索栏。我使用UISearchDisplayController来实现相同的功能。它还有一个带两个按钮的示波器栏。默认情况下,当我启动应用程序时,将显示范围栏。当我在搜索后单击取消按钮时,示波器条消失了。因此,即使在我按下“取消”按钮后,还有任何方法可以保留范围栏。我使用了以下代码,但它不起作用。
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsScopeBar:YES];
return YES;
}
谢谢:)
答案 0 :(得分:5)
我今天遇到了问题,我想我找到了解决方案。
你需要做两件事:
最终代码:
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
self.searchBar.showsScopeBar = YES;
[self.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchBar;
return YES;
}
答案 1 :(得分:4)
我自己过去几天也一直在与这些UISearchBarController
问题作斗争,我不得不说用UISearchBar做任何不寻常的事情的最好方法是不要使用UISearchDisplayController
来所有!
只需使用UISearchBar
和UISearchBarDelegate
方法并自行滚动,然后就可以将所有内容设置为完全符合您的需求。
这是我最近在一个项目中所做的。 - 示波器栏始终可见 - 我在输入文字时立即过滤 - 如果范围发生变化,我会立即过滤 - 我不需要时隐藏取消按钮 - 我不需要时隐藏键盘
// Filters the table when requested
- (void)filterContentForSearchBar:(UISearchBar *)searchBar
{
NSString *scope = [[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]];
NSString *search = [searchBar text];
NSMutableArray *predicates = [[NSMutableArray alloc] init];
if ([scope isEqualToString:@"Selected"])
{
[predicates addObject:[NSPredicate predicateWithFormat:@"selected == 1"]];
}
if (search && search.length) {
[predicates addObject:[NSPredicate predicateWithFormat:@"name contains[cd] %@", search]];
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
self.filteredObjectList = [self.objectList filteredArrayUsingPredicate:predicate];
[self.myTableView reloadData];
}
#pragma mark - UISearchBarDelegate Methods
// React to any delegate method we are interested in and change whatever needs changing
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = true;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = false;
[searchBar resignFirstResponder];
searchBar.text = nil;
[self filterContentForSearchBar:searchBar];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = false;
[searchBar resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self filterContentForSearchBar:searchBar];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self filterContentForSearchBar:searchBar];
}
效果很好:)
答案 2 :(得分:0)
这个answer更正确的方法是为返回结果添加逻辑:
@property (nonatomic) BOOL shouldHideFirstResponder; //assign YES in init
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
if (self.shouldHideFirstResponder) {
self.searchBar.showsScopeBar = YES;
[self.searchBar sizeToFit];
self.table.tableHeaderView = self.searchBar;
}
return self.shouldHideFirstResponder;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
self.shouldHideFirstResponder = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
self.shouldHideFirstResponder = YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
if (!self.searchBar.text || self.searchBar.text.length < 1) {
self.shouldHideFirstResponder = YES;
}
}