Search code examples
iosdelegatesdatasourceuipickerview

Loading Data into UIPIckerView?


I'm trying to build an app which works alongside a database of PDF's. What I need is for the app to look at all of the files (PDF's) which are stored and to pull the certain files out, check this against the database and then show a list of which PDF's are available on a UIPicker.

My first step would be that the app checks the files and creates a picker wheel which has been populated with the countries for which there is data in the directory.

To make this work I have created a standard format for the file names: 'Country-Airfield-Plate Name-Date.pdf', e.g. “UK-London Heathrow-ILS DME NDB 27-100214.pdf”. I've split this filename first at the full stop to get two strings: 'UK-London Heathrow-ILS DME NDB 27’ and ‘pdf’, and then we split the title at the hyphens to get ‘UK’, ‘London Heathrow’ and ‘ILS DME NDB 27’.

The app will check the first string for the country information, then the second for the airfield and will then display a list of the third strings for the appropriate airfield.

How can I First Fetch the data from database for a particular field for eg. country make a NSArray form that data and load that NSArray to UIPickerView, then use that array in UIPickerView Delegate and Datasource methods?


Solution

  • If you don't have any web service going and you are handling the documents yourself. You can hardcode a .plist file and put it on your website, download this small plist file every time the app opens, encrypt and save it to your documents directory.

    The plist is basically a NSDictionary with objects and keys. So your plist structure would look like this:

    {
        Countries =     (
                    {
                Airfields =             (
                                    {
                        Code = JNB;
                        Plates =                     (
                                                    {
                                Title = "Plate 1";
                                URL = "http://www.airport.com/za/airfield/plate1/document.pdf";
                            }
                        );
                        Title = "OR Tambo";
                    }
                );
                Title = "South Africa";
            }
        );
    }
    

    The above example only holds 1 object in each array in your case it would be more.

    Then use the following UIPickerView delegate assuming button.tag is called to change pickerview.tag and display correct array for the pickerview:

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
    {
        return [[self returnCorrectArrayForButton:pickerView.tag] count];
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [[[self returnCorrectArrayForButton:pickerView.tag] objectAtIndex:row] valueForKey:@"Title"];
    }
    
    // Handle these errors where user can click on any button. Airfields will be empty if no country selected. Plates will be empty when no Airfields selected. After loading all arrays, pressing random buttons might give wrong information.
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if (pickerView.tag == 1) {
    
            _airfieldArray = [[_countryArray objectAtIndex:row] objectForKey:@"Airfields"];
            [_countryButton setTitle:[[_countryArray objectAtIndex:row] valueForKey:@"Title"] forState:UIControlStateNormal];
    
        } else if (pickerView.tag == 2) {
    
            _plateArray = [[_airfieldArray objectAtIndex:row] objectForKey:@"Plates"];
            [_airfieldButton setTitle:[[_airfieldArray objectAtIndex:row] valueForKey:@"Title"] forState:UIControlStateNormal];
    
        } else if (pickerView.tag == 3) {
    
            // Show PDF in webview for selected plate
            [_plateButton setTitle:[[_plateArray objectAtIndex:row] valueForKey:@"Title"] forState:UIControlStateNormal];
            NSString *urlString = [[_plateArray objectAtIndex:row] valueForKey:@"URL"];
            NSLog(@"Selected Plate PDF URL : %@", urlString);
        }
    }
    
    - (NSArray *)returnCorrectArrayForButton:(NSUInteger)buttonIndex
    {
        if (buttonIndex == 1) {
            // Return Countries array
            return _countryArray;
    
        } else if (buttonIndex == 2) {
            // Return Airfield array
            return _airfieldArray;
    
        } else if (buttonIndex == 3) {
            // Return Plates array
            return _plateArray;
        }
        return nil;
    }