开发者

UISearchDisplayController - how to display search result with only by scope button selected but empty search string

The UISearchDisplayController is very handy and implementing search is pretty straightforward.

However, I bump into problem when, in my app, I want to display search result with empty search string but selected scope button.

It seems like it's a must to enter some search string in order to get the search result table being initialized and displayed.

开发者_如何学Go

Is there any ways to display search result immediately after user has picked a scope but not entered search word yet?

Thanks Bill


when you tap a new scope button the selectedScopeButtonIndex fires:

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

you could capture the title fire off your search here using:

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

Won't work on the initial scope index, but you could just fire off your search initially based on the last used selectedScopeButtonIndex


I was after the same thing and just found something in the Apple developer forums: The UISearchDisplayController is implemented in a way that the results table won't be shown until some text is entered. There's also a bug report about this: ID# 8839635.

I worked around it by putting a segmented control underneath the search bar, imitating the scope bar.


Here's a workaround that uses the scope buttons. The main thing is to add an extra character for the scope(s) that you want to show search results for automatically, but ensure that you remove it for the scope(s) that you do not want to do this.

You will need to implement searchBar:textDidChange as well as 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{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜