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
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];
try initialising all three arrarys in init method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With