我有一个字符串mild,medium,hot
。我用逗号分隔字符串作为分隔符。我也需要在pickerView中打印它。
我使用以下代码并将List计数成功3。
NSString *spList=[mdict objectForKey:@"spicinesstype"];
NSArray *list = [spList componentsSeparatedByString:@","];
NSLog(@"List count:%d",[list count]);
return [list count];
但是如何在pickerview中显示所有3个项目
答案 0 :(得分:5)
您应该将您的类设置为选择器视图的委托,然后为您的选择器实现这3个委托方法
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
答案 1 :(得分:2)
首先,你需要实现UIPickerViewDelegate和UIPickerViewDataSource,方法是将它们放在视图控制器头文件的@interface行的末尾。
像这样:@interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
接下来,您需要将视图控制器设置为选择器视图的委托和数据源。您可以通过添加以下行来在视图控制器的- (void)viewDidLoad
方法中执行此操作:
myPickerView.delegate = self;
myPickerView.dataSource = self;
或者,如果您正在使用它,可以在Interface Builder中将其链接起来。
然后,您需要在View Controller源文件中实现这些委托方法。
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [list count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [list objectAtIndex:row];
}