I am using a picker with two components. I want if I select a row in first component on the basis of selected component it shows the value of the corresponding data.
Usually you do it like that. You create a plist file containing the data of all your countries the result is something like
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>England</key>
<array>
<string>Chelsea</string>
<string>Arsenal</string>
</array>
<key>Spain</key>
<array>
<string>Barca</string>
<string>Real</string>
</array>
</dict>
</plist>
assuming the following are defined in the properties or somewhere
NSDictionary *countryClubs;
NSArray *countries;
NSArray *clubs;
you then do things like that
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistFile = [bundle URLForResource:@"myPListFile" withExtension:@"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistFile];
self.countryClubs = dictionary;
NSString *selectedCountry = [self.countries objectAtIndex:0];
NSArray *array = [countryClubs objectForKey:selectedCountry];
self.clubs = array;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0)
return [self.countries count];
return [self.clubs count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
if (component == 0)
return [self.countries objectAtIndex:row];
return [self.clubs objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (component == 0) {
NSString *selectedCountry = [self.countries objectAtIndex:row];
NSArray *array = [countryClubs objectForKey:selectedCountry];
self.clubs = array;
[picker selectRow:0 inComponent:1 animated:YES];
[picker reloadComponent:1];
}
}
This should help you. I hope there is not to much typo mistakes but you should at least get the general idea.
Look at this, i think it can help you
First of all make sure that you have connected your picker with IBOutlet .
Still if you get the issue you can use
[thePickerView reloadComponent:(NSInteger)component];
instead of
[picker reloadComponent:(NSInteger)component];
I think the problem is in your didSelectRow method. You check which country is already selected (in country_flag) before allowing the selection of a new country. I don't think there's any reason to do this.
Allow any country to be selected. Then set country_flag based on the row that was selected.
Just do country_flag = row+1;
You can then have your if statements to set up your label if that's important.
your code.. with minor modifications..
didSelectRow
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
[pickerView reloadComponent:1];
}
}
after that...
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if(component ==0)
{
return [Nations count];
}
else {
if ([club isEqualToString:@"Espana"]) {
return [Espana count];
}
if ([club isEqualToString:@"Germany"]) {
return [Germany count];
}
// if...
else {
return [England count];
}
}
return 0;
}
and
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(component ==0)
{
return [Nations objectAtIndex:row];
}
else {
if ([club isEqualToString:@"Espana"]) {
return [Espana objectAtIndex:row];
}
if ([club isEqualToString:@"Germany"]) {
return [Germany objectAtIndex:row];
}
//if....
else {
return [England objectAtIndex:row];
}
}
return 0;
}
UPD
in h file I have
@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
NSMutableArray *Nations;
NSMutableArray *England;
NSMutableArray *Espana;
NSMutableArray *Germany;
NSString *club;
}
after that.. you must connect de pickerView to yours ViewController (dataSource and delegate)
