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

Categories

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

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

1 Answer

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
            }       

        }   

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...