我已经关注了几个 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