UIPickerView无法正确显示数据(仅显示“?”)

时间:2013-05-04 17:15:52

标签: ios objective-c uipickerview uipickerviewcontroller uipickerviewdatasource

如果之前已经讨论过这个问题,请原谅我 我在UIPickerView中显示数据时出现问题,只显示问号“?”

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 个答案:

答案 0 :(得分:3)

好的,您的问题已解决,问题在于委托方法不正确,

您使用的方法(用于加载数据)不是委托方法,

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

尝试使用此方法,(必须工作)

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