1
votes

I'm stuck. I have called the PFQuery and I NSlog my "Categories" and it all comes fine in the debugger area.

Where I'm having trouble is using this data to populate my UIPickerView from Parse.com.

Here is what I came up with.

Note that I have the necessary methods already for the Picker, I just need to use the data from parse to populate it.

_pickerData is a NSArray that populates the picker and I thought I could equal to objects to populate but this didn't work.

EDIT: What I have so far...

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    [query whereKeyExists:@"Category"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _pickerData = objects;

            NSLog(@"%@",objects);
        }
        else {
            NSLog(@"error");
        }
    }];


    self.categoryPicker.dataSource = self;
    self.categoryPicker.delegate = self;


}


// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[row];
}
3

3 Answers

0
votes

Reload your picker view after assign objects to _pickerData

PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
[query whereKeyExists:@"Category"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        _pickerData = objects;

      [_categoryPicker reloadData];

      NSLog(@"%@",objects);
    }
    else {
        NSLog(@"error");
    }
}];
0
votes
PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    [query whereKeyExists:@"Category"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _pickerData = objects;

             // update pickerView
             [categoryPicker reloadAllComponents];

            NSLog(@"%@",objects);
        }
        else {
            NSLog(@"error");
        }
    }];

Use [categoryPicker reloadAllComponents]; to update the pickerView after the data is loaded.

Edit:

Set delegate & source // Connect data

self.categoryPicker.dataSource = self;
self.categoryPicker.delegate = self;

Add methods:

// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[row];
}
0
votes

Try this, as said above you need to reload your pickerView after your query finds the objects and loads the array. Also note this is an array of PFObjects, and you must use a key-value for the title, you can't just plug in a PFObject for the titleForRow: DataSource call.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.categoryPicker.dataSource = self;
    self.categoryPicker.delegate = self;

    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    [query whereKeyExists:@"Category"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _pickerData = objects;
            [self.categoryPicker reloadAllComponents];
        }
        else {
            NSLog(@"error");
        }
    }];
}


// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    PFObject *object = _pickerData[row];
    //Assuming "Category" is a string here for your title
    return object[@"Category"];
}