即使按下取消按钮后如何保留范围栏?

发布于 2024-12-27 14:11:12 字数 336 浏览 1 评论 0原文

我有一个 UITableView,顶部有一个搜索栏。我使用 UISearchDisplayController 来实现相同的功能。它还有一个带有两个按钮的范围栏。默认情况下,当我启动应用程序时,将显示范围栏。当我在搜索后单击取消按钮时,范围栏消失了。那么,即使在我按下“取消”按钮后,有什么方法可以保留范围栏吗?我使用了以下代码,但它不起作用。

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsScopeBar:YES];
    return YES;
}

谢谢 :)

I have a UITableView with a searchbar on the top. I used UISearchDisplayController for implementing the same. And also it has a scope bar with two buttons. In default when I launch the app, the scope bar will be displayed. When I click the cancel button after the searching, the scopebar disappeared. So is there any way to keep the scopebar even after I pressed the Cancel button. I used the following code but its not working.

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsScopeBar:YES];
    return YES;
}

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨光如昨 2025-01-03 14:11:12

我今天遇到了这个问题,我想我已经找到了解决方案。

您需要做两件事:

  1. 设置showsScopeBar后在searchBar上调用“sizeToFit”。这将确保 searchBars 框架正确设置以包含范围栏。
  2. 不幸的是,当 searchBar 调整大小并导致范围栏与第一个单元格重叠时,表视图似乎不喜欢它。要解决此问题,您可以将 tableHeaderView 重新设置为 searchBar (再次),这似乎可以解决重叠问题。

最终代码:

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = YES;
    [self.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchBar;
    return YES;
}

I've had the problem today and I think I've found a solution.

You need to do two things:

  1. Call 'sizeToFit' on the searchBar after setting showsScopeBar. This will ensure the searchBars frame is set correctly to include the scope bar.
  2. Unfortunately the table view doesn't seem to like it when the searchBar resizes and causes the scope bar to overlap the first cell. To solve this you can re-set the tableHeaderView to be the searchBar (again) which seems to fix the overlap problem.

Final code:

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = YES;
    [self.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchBar;
    return YES;
}
你げ笑在眉眼 2025-01-03 14:11:12

在过去的几天里,我自己也一直在与这类 UISearchBarController 问题作斗争,我不得不说,使用 UISearchBar 做任何不寻常的事情的最佳方法就是不使用 UISearchDisplayController代码>根本!

只需使用 UISearchBarUISearchBarDelegate 方法并推出您自己的方法,然后您就可以将所有内容设置为完全按照您想要的方式运行。

这是我在最近的一个项目中所做的。
- 范围栏始终保持可见
- 输入文本后我立即进行过滤
- 如果范围发生变化,我会立即过滤
- 当不需要时我隐藏取消按钮
- 不需要时我会隐藏键盘,

// 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];
}

效果很好:)

I have also been battling with these kinds of UISearchBarController problems the last few days myself, and I have to say the best way to do anything unusual with a UISearchBar is to not use a UISearchDisplayController at all!

Just use a UISearchBar and the UISearchBarDelegate methods and roll your own, then you can set everything all up to act exactly how you want.

Here what I did in one recent project.
- The scope bar always stays visible
- I filter immediately as text is entered
- I filter immediately if scope is changes
- I hide the cancel button when it's not needed
- I hide the keyboard when it's not needed

// 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];
}

Works great :)

錯遇了你 2025-01-03 14:11:12

这个答案更正确的方法是添加返回结果的逻辑:

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

More correct way of this answer is to add logic for return result:

@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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文