theJPE
theJPE

Reputation: 63

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

Upvotes: 6

Views: 10271

Answers (7)

Manann Sseth
Manann Sseth

Reputation: 2745

Your code is fine. Just need to change in object of "pickerView" name in method.

Replace

- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component

By,

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component

And,

- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component

By,

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component

Because personInfoPicker is the object of PickerView you used already in .h file, So it'll conflict while loading data for pickerView.

Hopefully, it'll help you.

Thanks.

Upvotes: 1

Naimish Karia
Naimish Karia

Reputation: 346

i Think your code is absolutely correct and you set datasource and delegate as well but you forget to reload uipickerview so in last of viewdidload reload pickerview that solve your problem

Upvotes: 1

MOHAN.C
MOHAN.C

Reputation: 315

I think you did't allocated the array used as data source of picker e.g

 NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil];

Upvotes: 0

rockstarberlin
rockstarberlin

Reputation: 1853

I´ve copied your initial code and found your bug now. There is a typo in the delegate method name

instead of

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

it must be

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

If you write pickerview it will declare a new method in your class which will never be called. So simple but anoying anyway :-)

Upvotes: 1

MOHAN.C
MOHAN.C

Reputation: 315

can you try this below code.

`NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil];
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{


    if ([pickerView isEqual:_month_picker_view]) {
        return [_month_ary count];
    }
    return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{


    if ([pickerView isEqual:_month_picker_view])
    {
        return [_month_ary objectAtIndex:row];
    }
    return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{


    if ([pickerView isEqual:_month_picker_view]) {
        _Pay_Month_txt.text=[_month_ary objectAtIndex:row];
        deal_cridit_card_month_str = [_month_ary objectAtIndex:row];
    }
}`

Upvotes: 2

Filip Radelic
Filip Radelic

Reputation: 26683

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];

Upvotes: 18

Durgaprasad
Durgaprasad

Reputation: 1951

try initialising all three arrarys in init method.

Upvotes: 2

Related Questions