UIPickerView的默认高度为162;
显示数据需要设置数据源,也有两种方式:成为数据源、遵守协议;
实现数据源的两个方法:多少列,一列有多少行;
设置代理为控制器,实现某一列某一行的数据是什么;
与UITableView的数据源协议和委托协议相似;
贴代码了:
- (void)initWithPickerViewOther
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"freshFood.plist" ofType:nil];
_foods = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"_foods : %@", _foods);
for (NSUInteger component = 0 ; component < _foods.count; component++)
{
[self pickerView:nil didSelectRow:0 inComponent:component];
}
NSLog(@"%@", _foods);
}
- (void)initWithPickerViewUI
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width-40, 30)];
[btn setTitle:@"随机" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor orangeColor]];
[btn addTarget:self action:@selector(random:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 150, [UIScreen mainScreen].bounds.size.width-40, 162)];
_pickerView.showsSelectionIndicator = YES;
_pickerView.delegate = self;
_pickerView.dataSource = self;
[self.view addSubview:_pickerView];
_fruitLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 320, [UIScreen mainScreen].bounds.size.width-40, 38)];
_fruitLabel.textColor = [UIColor darkTextColor];
_fruitLabel.font = [UIFont systemFontOfSize:18.0];
[self.view addSubview:_fruitLabel];
_stapleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 368, [UIScreen mainScreen].bounds.size.width-40, 38)];
_stapleLabel.textColor = [UIColor darkTextColor];
_stapleLabel.font = [UIFont systemFontOfSize:18.0];
[self.view addSubview:_stapleLabel];
_drinkLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 416, [UIScreen mainScreen].bounds.size.width-40, 38)];
_drinkLabel.textColor = [UIColor darkTextColor];
_drinkLabel.font = [UIFont systemFontOfSize:18.0];
[self.view addSubview:_drinkLabel];
}
- (void)random:(UIButton *)sender
{
NSLog(@"a");
for (NSUInteger component = 0; component < _foods.count; component++)
{
NSUInteger total = [_foods[component] count];
NSUInteger randomNumber = arc4random() % total;
NSUInteger oldRow = [_pickerView selectedRowInComponent:0];
while (oldRow == randomNumber)
{
randomNumber = arc4random() % total;
}
// 滑动到指定的每一行
[_pickerView selectRow:randomNumber inComponent:component animated:YES];
// 选中某一行
[self pickerView:nil didSelectRow:randomNumber inComponent:component];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return _foods.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *array = [_foods objectAtIndex:component];
return array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *array = [_foods objectAtIndex:component];
NSString *title = [array objectAtIndex:row];
return title;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *title = [[_foods objectAtIndex:component] objectAtIndex:row];
if (0 == component)
{
_fruitLabel.text = [NSString stringWithFormat:@"您选择的水果是:%@", title];
}
else if (1 == component)
{
_stapleLabel.text = [NSString stringWithFormat:@"您选择的蔬菜是:%@", title];;
}
else
{
_drinkLabel.text = [NSString stringWithFormat:@"您选择的饮料是:%@", title];;
}
}
网友评论