Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

iphone - iOS Search Results Order

I have a UISearchBar that searches a table of data. When a search is typed in, the results are displayed in an order like so:

Starts typing 'ch...' 1. Ache 2. Cherries 3. Choice

It makes more sense to me (and for my app) that 'Cherries' and 'Choice' would be at the top of the results, because 'ch' and not 'ach' was typed in. Is this something that can be changed programatically, or is it just the way iOS searches work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to give the search option as NSAnchoredSearch

NSRange searchRange = [sortedString rangeOfString:searchText options:NSAnchoredSearch];

Some of the search method listed below

NSCaseInsensitiveSearch

NSLiteralSearch

NSBackwardsSearch

NSAnchoredSearch

NSNumericSearch

NSDiacriticInsensitiveSearch

NSWidthInsensitiveSearch

NSForcedOrderingSearch

NSRegularExpressionSearch

Eg:

- (void)search {

    NSString *searchText = [searchBar.text lowercaseString];


    for (int index = 0; index < [availableCollectionArray count]; index++) {

        NSArray *tempArray = [availableCollectionArray objectAtIndex:index];

        for (int tempIndex = 0; tempIndex < [tempArray count] ; tempIndex++) {

            NSString *sortedString = [tempArray objectAtIndex:tempIndex];

            NSRange searchRange = [sortedString rangeOfString:searchText options:NSAnchoredSearch];

            if (searchRange.length > 0)
            {
                [sortedArray addObject: sortedString];  //add the string which starts from searchBar.text
            }       

        }   

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...