Domanda

Every example I find shows me how to populate a UIPickerView in viewDidLoad. However, when I try to populate the picker with items from an HTTP Request in another function, it ends up being blank (even though I've tested that the data actually does come back as I want it).

- (IBAction)btnClick:(id)sender
{
    NSDictionary *courseNames;
    if(![_txtBox.text isEqual:@""]) //if not empty
    {
        courseNames = [self retrieveCourseNamesForSemester:_txtBox.text];
        for (NSString *key in courseNames)
        {
            NSString *val = [NSString stringWithFormat:@"%@ - %@",key,[courseNames objectForKey:key]];
            _txtView.text = val;
            [courseArray addObject:val];
        }
    }
    _coursePicker.hidden=false;
    [_txtBox resignFirstResponder];
}

_coursePicker is created on the storyboard itself, so it's not added programmatically or anything. I'm adding to the array courseArray, which is assigned in the UIPickerView methods, as follows:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

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

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    return [courseArray objectAtIndex:row];
}

How do I repopulate the UIPickerView at this point?

È stato utile?

Soluzione

After you get your data and before _coursePicker.hidden = NO; try calling

[_coursePicker reloadAllComponents];

That should make the picker repopulate with your data.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top