6

我已经关注了几个 youtube 教程以及这里的建议,但我似乎无法让我的 UIPickerView 显示其数据。我正在使用三个数组填充选择器,我认为我已经正确实现了代码,但显然有问题。选择器正确显示元素的数量,问题是它们都显示为“?”

我尝试将选择器的委托设置为 self,但这也不起作用。提前致谢。

这是我的 .h 文件:

@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

这是我的 .m 文件:

@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
4

7 回答 7

18

您需要设置选择器视图dataSourcereloadAllComponents在填充数组后调用。

self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];
于 2013-03-11T07:32:57.627 回答
2

尝试在 init 方法中初始化所有三个数组。

于 2013-03-11T07:28:28.137 回答
2

你能试试下面的代码吗?

`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];
    }
}`
于 2013-03-12T13:03:15.177 回答
1

你的代码很好。只需要在方法中更改“pickerView”名称的对象。

代替

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

经过,

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

和,

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

经过,

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

因为personInfoPickerPickerView你已经在.h文件中使用的对象,所以在为.h文件加载数据时会发生冲突pickerView

希望它会帮助你。

谢谢。

于 2013-03-11T08:28:38.857 回答
1

我认为您的代码是绝对正确的,并且您也设置了datasourcedelegate但是您忘记重新加载uipickerview,因此在最后一次viewdidload重新加载pickerview时可以解决您的问题

于 2013-03-11T08:58:20.220 回答
1

我已经复制了您的初始代码,现在发现了您的错误。委托方法名称中有错字

代替

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

一定是

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

如果您编写pickerview,它将在您的类中声明一个永远不会被调用的新方法。如此简单但无论如何都很烦人:-)

于 2013-04-23T20:09:54.520 回答
0

我认为您没有分配用作选择器数据源的数组,例如

 NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil];
于 2013-03-12T12:57:13.000 回答