代码之家  ›  专栏  ›  技术社区  ›  billibala

UISearchDisplayController-如何在只选择了“按范围”按钮但搜索字符串为空的情况下显示搜索结果

  •  9
  • billibala  · 技术社区  · 16 年前

    然而,在我的应用程序中,当我想用空搜索字符串显示搜索结果,但选择了范围按钮时,我遇到了一个问题。

    似乎必须输入一些搜索字符串才能得到初始化和显示的搜索结果表。

    谢谢 比尔

    3 回复  |  直到 16 年前
        1
  •  2
  •   Nik Burns    15 年前

    当您点击一个新的范围按钮时,selectedScopeButtonIndex将触发:

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption;
    

    您可以使用以下方法在搜索中捕获标题:

    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]
    

        2
  •  1
  •   Sebastian    15 年前

    我也在追求同样的东西,只是在苹果开发者论坛上发现了一些东西:the UISearchDisplayController

    我在搜索栏下面放了一个分段控件,模仿范围栏来解决这个问题。

        3
  •  0
  •   Wil Macaulay    13 年前

    你需要实施 searchBar:textDidChange searchBar:selectedScopeButtonIndexDidChange:

    // scope All doesn't do a search until you type something in, so don't show the search table view
    // scope Faves and Recent will do a search by default
    #define kSearchScopeAll 0
    #define kSearchScopeFaves 1
    #define kSearchScopeRecent 2
    
    // this gets fired both from user interaction and from programmatically changing the text
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
        [self initiateSearch];
    }
    
    
    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
        NSString *searchText = self.searchDisplayController.searchBar.text;
        // if we got here by selecting scope all after one of the others with no user input, there will be a space in the search text
    
        NSString *strippedText = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ((selectedScope == kSearchScopeAll) && (strippedText.length == 0) && (searchText.length != 0)){ 
            self.searchDisplayController.searchBar.text = @"";
        } else {
            [self initiateSearch];
        }
    }
    
    -(void)initiateSearch{
        NSString *searchText = self.searchDisplayController.searchBar.text;
        NSInteger scope = self.searchDisplayController.searchBar.selectedScopeButtonIndex;
        if ((searchText.length == 0) && (scope != kSearchScopeAll)){
            self.searchDisplayController.searchBar.text = @" ";
        }
        switch (scope) {
            case kSearchScopeAll:
                [self searchAll:searchText];
                break;
            case kSearchScopeFaves:
                [self searchFavorites:searchText];
                break;
            case kSearchScopeRecent:
                [self searchRecents:searchText];
                break;
    
            default:
                break;
        }
    }
    
    // assume these trim whitespace from the search term
    -(void)searchAll:(NSString *)searchText{
    }
    
    -(void)searchFavorites:(NSString *)searchText{
    }
    
    -(void)searchRecents:(NSString *)searchText{
    }
    
    推荐文章