iOS Search Results Order

后端 未结 1 1115
误落风尘
误落风尘 2020-12-18 17:55

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. Che

相关标签:
1条回答
  • 2020-12-18 18:11

    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
                }       
    
            }   
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题