Search code examples
iosobjective-cuipickerview

UIPickerView not displaying data or calling viewForRow


I am dynamically adding data to a pickerview as information becomes available. Though I am adding data to my array and calling [picker reloadAllComponents] the pickerview does not show any data.

I also noticed that neither of the following Delegates are getting called:

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

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

Here is my viewDidLoad:

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

  _pickerData = [[NSMutableArray alloc] init];
  _ActiveDevice = [[NSString alloc]init];

  // Connect data

  self.DevicePicker.dataSource = self;
  self.DevicePicker.delegate = self;

} 

Here is the method that loads my hidden PickerView (I have it defaulted to hide until a button is pushed)

- (IBAction)FanDrop:(id)sender {
    [self printFromCoreData];
    [self.DevicePicker reloadAllComponents];
    if (_DeviceBox) _DeviceBox.hidden = !_DeviceBox.hidden;
}

I load the array the is the datasource for the DevicePicker using what is found in coredata This all seems to work as I can find information when logging what is in the array. Also when clicking the accept button even though nothing is showing the correct data gets loaded thus telling me that everything but the display portion of my code is working correct.

Any insight - I'm open to try almost anything.

After one comment I realized I neglected to include, in my question, the delegates. SO here they are:

// 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
{
    //return _pickerData[row];
    return [_pickerData objectAtIndex:row];
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    return [_pickerData objectAtIndex:row];
}

Here is how I am adding the array pickerData:

@interface CMTViewController ()
{
    NSMutableArray *_pickerData;
}

Here is the CoreData method I use to load _pickerData:

-(void)printFromCoreData{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"DeviceData"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];


    for (NSManagedObject *info in _devices) {

    NSString *item = [NSString stringWithFormat:@"%1$@ %2$@",[info valueForKey:@"name"], [info valueForKey:@"ipAddress"]];

        if (![_pickerData containsObject:item]) {
            [_pickerData addObject:item];
        }
    }

     NSLog(@"_pickerData length %lu", (unsigned long)[_pickerData count]);
    [self.DevicePicker reloadAllComponents];
}

Here is the view of what happens in the simulator (or on my phone) when I push the fans button:

enter image description here


Solution

  • Fuji sir,

    My recommendation would be as follows based on our conversation:

    - (void)viewDidLoad
    {
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
      [self printFromCoreData];
    _ActiveDevice = [[NSString alloc]init];
    
    // Connect data
    
    self.DevicePicker.dataSource = self;
    self.DevicePicker.delegate = self;
    } 
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
    return _pickerData.count;
    }
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
    return 1;
    }
    
    - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
    return _pickerData[row];
    }
    

    And don't include reusingView unless you want to change the layout and add custom labels etc