UIPickerView not showing data correctly (Only Show "?")

1.9k Views Asked by At

Forgive me if this question has been discussed previously.
I have a problem displaying data in UIPickerView, it just display question mark "?".

header file.h

#import <UIKit/UIKit.h>
@interface CustomPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
    NSMutableArray *arrayColors;
}

@property (strong, nonatomic) IBOutlet UIPickerView *kotaPicker;

@end

impl.m

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayColors count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row {
    return [arrayColors objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"The Data is %@", [arrayColors objectAtIndex:row]);
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    arrayColors = [[NSMutableArray alloc] init];
    [arrayColors addObject:@"Red"];
    [arrayColors addObject:@"Orange"];
    [arrayColors addObject:@"Yellow"];
    [arrayColors addObject:@"Green"];
    [arrayColors addObject:@"Blue"];
    [arrayColors addObject:@"Indigo"];
    [arrayColors addObject:@"Violet"];
    // Do any additional setup after loading the view from its nib.
}
1

There are 1 best solutions below

1
On

Ok, Your problem is solved, a problem is with an incorrect delegate method,

The method you have used (for loading data) is not a delegate method,

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

Try this method instead, (must work)

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
     return [arrayColors objectAtIndex:row];
}