我已经关注了几个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
答案 0 :(得分:18)
您需要设置选择器视图的dataSource
并在填充数组后调用reloadAllComponents
。
self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];
答案 1 :(得分:2)
尝试在init方法中初始化所有三个数组。
答案 2 :(得分: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];
}
}`
答案 3 :(得分: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
由于personInfoPicker
是您在.h文件中使用的PickerView
的对象,因此在加载pickerView
的数据时会发生冲突。
希望它会帮助你。
感谢。
答案 4 :(得分:1)
我认为您的代码绝对正确,您也设置了datasource
和delegate
,但忘了重新加载uipickerview
,因此在viewdidload
重新加载{{1}的最后解决你的问题
答案 5 :(得分:1)
我已经复制了您的初始代码,现在发现了您的错误。委托方法名称中有拼写错误
而不是
- (NSString *)pickerview:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
一定是
- (NSString *)pickerView:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
如果您编写pickerview,它将在您的类中声明一个永远不会被调用的新方法。这么简单但很烦人: - )
答案 6 :(得分:0)
我认为你没有分配用作选择器数据源的数组,例如
NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil];