xcode UIPickerView 不显示数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15333234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIPickerView not displaying data
提问by theJPE
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 '?'
我已经关注了几个 youtube 教程以及这里的建议,但我似乎无法让我的 UIPickerView 显示其数据。我正在使用三个数组填充选择器,我认为我已经正确实现了代码,但显然有些问题。选择器正确显示元素的数量,问题是它们都显示为“?”
I've tried setting the picker's delegate to self, but that didn't work either. Thanks in advance.
我试过将选择器的委托设置为 self,但这也不起作用。提前致谢。
Here's my .h file:
这是我的 .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
Here's my .m file:
这是我的 .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
回答by Filip Radelic
You need to set picker view's dataSource
and call reloadAllComponents
after populating arrays.
您需要在填充数组后设置选择器视图dataSource
和调用reloadAllComponents
。
self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];
回答by Durgaprasad
try initialising all three arrarys in init method.
尝试在 init 方法中初始化所有三个数组。
回答by MOHAN.C
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];
}
}`
回答by rockstarberlin
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 :-)
如果您编写pickerview,它将在您的类中声明一个永远不会被调用的新方法。如此简单但无论如何都很烦人:-)
回答by MS.
Your code is fine. Just need to change in object of "pickerView" name in method.
你的代码没问题。只需要在方法中更改“pickerView”名称的对象。
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
.
因为你personInfoPicker
的对象PickerView
已经在.h文件中使用了,所以在加载.h文件时会发生冲突pickerView
。
Hopefully, it'll help you.
希望,它会帮助你。
Thanks.
谢谢。
回答by Naimish Karia
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
我认为你的代码是绝对正确的,你也设置了datasource
,delegate
但是你忘记重新加载uipickerview
所以在最后viewdidload
重新加载pickerview
解决了你的问题
回答by MOHAN.C
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];