Dependent UIPickerView

Does anyone know how to make a dependent UIPickerView. For example, when I select line 2 of component 1, do the names for component 2 change?

I looked on the Internet, there is no real answer, I tried to use if and switch statements, but they just crash.

+4
source share
5 answers

It depends on how you are going to store the data. For example, if you have an array as the value of a dictionary key, and this dictionary has different such keys, the first column will be the keys, and when you select it, you will display the array in another column (component). - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView method should return 2. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component method, you need to specify the number of keys in the dictionary for component 1 and the number of selected array the current key. eg,

 if(component==0) return [[DICTIONARY allKeys] count]; else return [[DICTIONARY objectForKey:@"SELECTED_KEY"] count]; 

Then

 - (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]; } } 

and

 - (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; } 
+6
source

I am sure that this can be done using a combination of the pickerView: didSelectRow: inComponent: and reloadComponents delegate method.

0
source

I hope this helps, I had a similar situation, my requirement was when I select the name of the country in the component, the cities of the country should be loaded in the second component. So, what I did, I had 2 pickerview side by side, and when the user selects a row in the country picker, I re-initiated the second picker with the corresponding data.

  - (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)   
     {


     }   
 }


0
source

I think you like to change the corresponding element of one component when changing another. This may be the best solution.

code:

 - (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]; } } 

Note. When you use this code, the component values ​​must be held by two NSArray, and the order of the corresponding dependent values ​​must be the same in both arrays.

0
source

you need to set the conditions for each row of the first component (with index 0) and open the switch to select each index, I did this and worked for me:

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) } } 

remember: YOU numberOfRowsInComponent:(NSInteger)component IT ALSO WITH -... numberOfRowsInComponent:(NSInteger)component and -... didSelectRow:(NSInteger)row inComponent:(NSInteger)component and in its last, in the row selection in component 0, call [pikcerIBOutlet reloadComponent:1]

0
source

Source: https://habr.com/ru/post/1343762/


All Articles