Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 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 Reply

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
            }       

        }   

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...