是否有人知道如何制作依赖UIPickerView。例如,当我选择组件1的第2行时,组件2的标题会发生变化吗?
我在互联网上看到没有真正的答案,我尝试使用if和switch语句,但它们只是崩溃。
答案 0 :(得分:6)
这取决于您将如何保存数据。例如,如果您将数组作为字典键的值,并且该字典具有不同的此类键,则第一列将是键,而在选择一列时,您将在另一列中显示该数组(组件) )。 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
方法应返回2。
在
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
方法,您需要在组件1的字典中给出键的数量,以及当前所选键的数组的计数。
例如
if(component==0) return [[DICTIONARY allKeys] count];
else return [[DICTIONARY objectForKey:@"SELECTED_KEY"] count];
然后,
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedIndex = [pickerView selectedRowInComponent:0];
if (component == 1 && !(selectedIndex < 0)) {
[pickerView reloadComponent:2];
[pickerView selectRow:0 inComponent:2 animated:YES];
}
}
和
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerRow = (view != nil) ? view : [[[UILabel alloc] initWithFrame:CGRectMake(5, 0, 115, 60)] autorelease];
pickerRow.font = [UIFont boldSystemFontOfSize:14];
pickerRow.textAlignment = UITextAlignmentLeft;
pickerRow.backgroundColor = [UIColor clearColor];
pickerRow.textColor = [UIColor blackColor];
pickerRow.numberOfLines = 0;
if (component == 0) {
pickerRow.text = @"DICTIONARY_ROW'th_KEY";
}
else {
pickerRow.text = [[dictionary objectForKey:@"SELECTED_KEY"] objectAtIndex:row];
}
return pickerRow;
}
答案 1 :(得分:0)
我确信这可以使用委托方法pickerView:didSelectRow:inComponent:和reloadComponents的组合来完成。
答案 2 :(得分:0)
希望这有帮助,我有类似的情况,我的要求是当我在组件中选择国家/地区名称时,应该在第二个组件中加载该国家/地区的城市。所以我做的是,我有2个pickerview并排,当用户在国家选择器中选择一行时,我重新启动了第二个选择器并提供了适当的数据。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (pickerView == countryPickerView) { if(PickerViewCity) { [PickerViewCity removeFromSuperview]; [PickerViewCity release]; PickerViewCity=nil; } PickerViewCity = [[UIPickerView alloc] initWithFrame:CGRectMake(160, 38, 320, 240)]; PickerViewArrayCity=[[countryPickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]]objectForKey:@"nodeChildArray"]; //NSLog(@"%@",PickerViewArrayCity); PickerViewCity.showsSelectionIndicator = YES; // note this is default to NO PickerViewCity.delegate = self; PickerViewCity.dataSource = self; CGAffineTransform s20 = CGAffineTransformMakeScale(0.5, 0.58); CGAffineTransform t21 = CGAffineTransformMakeTranslation(-80,-50); PickerViewCity.transform = CGAffineTransformConcat(s20, t21); PickerViewCity.layer.masksToBounds = YES; [someview addSubview:PickerViewCity]; } if (pickerView == PickerViewCity) { } }
答案 3 :(得分:0)
我认为,您希望在更改另一个组件时更改一个组件的相应项。这可能是一个更好的解决方案。
代码:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (component == 0) {
//Here First Part of the Pickerview is represented using 0.
// dependentPicker is the Pickerview outlet
[dependentPicker selectRow:row inComponent:1 animated:YES];
[dependentPicker reloadComponent:1];
}
else
{
[dependentPicker selectRow:row inComponent:0 animated:YES];
[dependentPicker reloadComponent:1];
}
}
注意:使用此代码时,组件值必须由两个NSArray保持,并且两个数组中相应依赖值的顺序必须相同。
答案 4 :(得分:0)
你需要为第一个组件的每一行设置条件(索引为0)并打开一个开关来选择每个索引,我做了它并为我工作:
on ... titleForRow:(NSinteger)row forComponent(NSInteger)component
{
if (component == 0) // this is first component from left to right
return [arrayForFirstComponent objectAtIndex:row];
if (component == 1) // this is second component from left to right
switch([pickerIBOutletAsYouNamedIt selectedRowInComponent:0]) // here you set content of component two depending on which row is selected from component 1
{
case 0: // remember index starts at 0
return [arrayForComponent2ForRow1inComponent1 objectAtIndex:row];
break;
//... and so on for each row in the component 1 (with index 0)
}
}
记住:你也必须这样做
- ...... numberOfRowsInComponent:(NSInteger)component
与
- ...... didSelectRow:(NSInteger)row inComponent:(NSInteger)component
在他的最后一个中,在组件0中选择行调用[pikcerIBOutlet reloadComponent:1]