Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPickerView not displaying data

I've followed a couple youtube tutorials as well as advice on here but I can't seem to get my UIPickerView to display its data. I'm populating the picker using three arrays and I thought I've implemented the code correctly but obviously something is wrong. The picker displays the number of elements correctly, the problem is that they are all displayed as '?'

I've tried setting the picker's delegate to self, but that didn't work either. Thanks in advance.

Here's my .h file:

@interface BACcalcViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
@property(strong, nonatomic) IBOutlet UIPickerView *personInfoPicker;
@property(strong, nonatomic) NSArray *heightsArray;
@property(strong, nonatomic) NSArray *weightsArray;
@property(strong, nonatomic) NSArray *timeArray;
@end

Here's my .m file:

@interface BACcalcViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@end

@implementation BACcalcViewController


@synthesize personInfoPicker, heightsArray, weightsArray, timeArray;
- (void)viewDidLoad
{

[super viewDidLoad];

heightsArray = [NSArray arrayWithObjects:
                         @"5ft 0in",
                         @"5ft 1in",
                         @"5ft 2in",
                         @"5ft 3in",
                         @"5ft 4in",
                         @"5ft 5in",
                         @"5ft 6in",
                         @"5ft 7in",
                         @"5ft 8in",
                         @"5ft 9in",
                         @"5ft 10in",
                         @"5ft 11in",
                         @"6ft 1in",
                         @"6ft 2in",
                         @"6ft 3in",
                         @"6ft 4in",
                         @"6ft 5in",
                         @"6ft 6in",
                         @"6ft 7in",
                         @"6ft 8in",
                         nil];

weightsArray = [NSArray arrayWithObjects:
                     @"100",
                     @"110",
                     @"120",
                     @"130",
                     @"140",
                     @"150",
                     @"160",
                     @"170",
                     @"180",
                     @"190",
                     @"200",
                     @"210",
                     @"220",
                     @"230",
                     @"240",
                     @"250",
                     @"260",
                     nil];


timeArray = [NSArray arrayWithObjects:
             @"1",
             @"2",
             @"3",
             @"4",
             @"5",
             @"6",
             nil];
}



- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)personInfoPicker
{
    return 3;
}


- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component
{
    if (component == 0) 
        return [heightsArray count];
    else if (component == 1)
        return [weightsArray count];
    else
        return [timeArray count];
}

- (NSString *)pickerview:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{    
    if (component == 0)
        return [heightsArray objectAtIndex:row];
    else if (component == 1)
        return [weightsArray objectAtIndex:row];
    else
        return [timeArray objectAtIndex:row];
}
@end
like image 530
theJPE Avatar asked Mar 11 '13 07:03

theJPE


2 Answers

You need to set picker view's dataSource and call reloadAllComponents after populating arrays.

self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];
like image 135
Filip Radelic Avatar answered Oct 17 '22 04:10

Filip Radelic


try initialising all three arrarys in init method.

like image 20
Durgaprasad Avatar answered Oct 17 '22 05:10

Durgaprasad