Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to sort country list? #36

Open
DyingPeter opened this issue Sep 19, 2016 · 1 comment
Open

How to sort country list? #36

DyingPeter opened this issue Sep 19, 2016 · 1 comment

Comments

@DyingPeter
Copy link

No description provided.

@lukeswitz
Copy link

lukeswitz commented Oct 10, 2016

Use a textfield and filter the _countrynames array, this should get you started.


   //Make a clean string
    if (textField.text != NULL) {
        theNewString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    }

    //Search the array for our match
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"SELF BEGINSWITH[c] %@",theNewString];

    autoCompleteFilterArray = [countryData filteredArrayUsingPredicate:predicate];

    //Now sort the filtered array
    sortedArray = [autoCompleteFilterArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    //Nothing found
    if (sortedArray.count == 0) {
        NSLog(@"No matches");    
    }
    else{  
       //fuzzy match & handle picker selection
            theNewString = [sortedArray firstObject];

            //Search the array to make sure we have that country
            BOOL isTheObjectThere = [countryData containsObject: theNewString];
            NSUInteger index = [countryData indexOfObject:theNewString];

            //Take our data and spin to a country we have sorted through
            if (isTheObjectThere) {
                if (textField == field1) {
                    [picker1 selectRow:index inComponent:0 animated:YES];
                    index1 = index;
                    firstSelectedName = [countryData objectAtIndex:index1];

                    NSArray *codes = [CountryPicker countryCodes];                    
                    NSString *selCode1 = [codes objectAtIndex:index];

                    imgPath1 = [NSString stringWithFormat:@"CountryPicker.bundle/%@",
                                selCode1];

                    // Get country code to convert for api pull later
                    firstCode = selCode1;
                    }
            }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants