PDA

View Full Version : uipicker component and row selection




mikeyman77
Feb 3, 2012, 06:29 AM
Hello,

I'm quite new to this iPhone programming and can up to now figure out and get code working but i cant seem to get this working. I wonder if anyone can help !!!


I have a uipicker with 2 components. 0 & 1

I want a text label to display information related to the Uipicker row combination. For example if i select row 37 on component 0 and row 2 on component 1 it will display info which would be different if i selected row 12 on component 0 with row 1 on component 1.

so i can show text related to the first component but nothing when selecting component 1.

Component 0 is loaded with a Array which is constant.
Component 1 is loaded with different arrays depending on the selected row in component 0.

Below is what i've got and cant figure out:






if ((component == 0 && row == 93) && (component== 1 && row ==0)){
[mypickerView selectRow:0 inComponent:1 animated:YES];
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"1 hour" ]autorelease]; }


else if ((component==0 && row == 94) && (component==1 && row == 1)){
label1.text =[[[NSString alloc] initWithFormat:@"2 hour" ]autorelease];}



else if ((component==0 && row == 94) && (component==1 && row == 2)){
label1.text =[[[NSString alloc] initWithFormat:@"3 hour" ]autorelease];}


else if ((component==0 && row == 94) || (component==1 && row == 3)){
label1.text =[[[NSString alloc] initWithFormat:@"4 hour" ]autorelease];}


else if ((component==0 && row == 94) || (component==1 && row == 4)){
label1.text =[[[NSString alloc] initWithFormat:@"5 hours" ]autorelease];}


else if ((component==0 && row == 94) || (component==1 && row == 5)){
label1.text =[[[NSString alloc] initWithFormat:@"6 hours" ]autorelease];}







if guess i asking how do i write the combination of (component 0 with row + component 1 + row ) ???


Thank you



dejo
Feb 3, 2012, 08:56 AM
Two things:

1) Where does the code you supplied reside? How is it used?

2) You talk about having arrays to load the components but then don't use them in your code-snippet. Why not?

mikeyman77
Feb 3, 2012, 04:37 PM
sorry this is my code:

@synthesize mypickerView;

-(IBAction)pushBack {
[self dismissModalViewControllerAnimated:YES];
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{


[super viewDidLoad];

model = [[NSMutableArray alloc] init ];
[model addObject:@"G645"];
[model addObject:@"G756"];
[model addObject:@"G976"];
[model addObject:@"GP563"];
[model addObject:@"GPS456"];
[model addObject:@"GPS459"];
[model addObject:@"GP12344"];
[model addObject:@"HT7893"];
[model addObject:@"HT7854"];
[model addObject:@"Z4569"];


equip = [[NSMutableArray alloc] init ];
[equip addObject:@"equip QW35"];
[equip addObject:@"equip Q7462"];
[equip addObject:@"equip V67"];
[equip addObject:@"equip FG87"];
[equip addObject:@"equip E6743"];
[equip addObject:@"equip Y678"];


equip1 = [[NSMutableArray alloc] init ];
[equip1 addObject:@"equip Z684"];
[equip1 addObject:@"equip H784"];
[equip1 addObject:@"equip I744"];
[equip1 addObject:@"equip P8494];
[equip1 addObject:@"equip Y848"];
[equip1 addObject:@"equip I8998"];



equip2 = [[NSMutableArray alloc] init ];
[equip2 addObject:@"no equip"];



[mypickerView selectRow:0 inComponent:0 animated:YES];



// Do any additional setup after loading the view from its nib.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
return 2;}




- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component
{
if (component == 0) {
return [model count];
}

else if (component == 1) {

if ([mypickerView selectedRowInComponent:0] == 2) {
return [equip count];
}

else if ([mypickerView selectedRowInComponent:0] == 3) {
return [equip1 count];
}

else if ([mypickerView selectedRowInComponent:0] == 4) {
return [equip1 count];
}

else if ([mypickerView selectedRowInComponent:0] == 6) {
return [equip count];
}
else {return [equip2 count];}
}return 0;}



- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{


if (component == 0) {

return [necproj objectAtIndex:row];
}

else if (component == 1) {

if ([mypickerView selectedRowInComponent:0] == 2) {
return [equip objectAtIndex:row];
}

else if ([mypickerView selectedRowInComponent:0] == 3) {
return [equip1 objectAtIndex:row];
}

else if ([mypickerView selectedRowInComponent:0] == 4) {
return [equip1 objectAtIndex:row];
}

else if ([mypickerView selectedRowInComponent:0] == 6) {
return [equip objectAtIndex:row];
}
else {return [equip2 objectAtIndex:row];}
}return 0;}





-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {






if (component == 0 && row == 0){
[mypickerView reloadComponent:1];
[mypickerView selectRow:0 inComponent:1 animated:NO];
label1.text =[[[NSString alloc] initWithFormat:@"1 hour" ]autorelease];}


if ((component == 0 && row == 1)|| (component==1 && row ==0)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"1 hour" ]autorelease];}


if ((component == 0 && row == 2)|| (component==1 && row ==0)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"2 hour" ]autorelease];}


else if ((component==modelComponent && row == 2) || (component==lensComponent && row == 1)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"3 hour ]autorelease];}



else if ((component==modelComponent && row == 2) || (component==lensComponent && row == 2)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"5 hour" ]autorelease];}


else if ((component==modelComponent && row == 2) || (component==lensComponent && row == 3)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"7 hour" ]autorelease];}



else if ((component==modelComponent && row == 2) || (component==lensComponent && row == 4)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"9 hour" ]autorelease];}



else if ((component==modelComponent && row == 2) || (component==lensComponent && row == 5)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"3.5 hour" ]autorelease];}



else if ((component==modelComponent && row == 2) || (component==lensComponent && row == 6)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"33 hour" ]autorelease];}






if ((component == 0 && row == 3)|| (component==1 && row ==0)){
[mypickerView reloadComponent:1];
label1.text =[[[NSString alloc] initWithFormat:@"4 hours" ]autorelease];}






so on and so on .................................










- (void)dealloc {

[super dealloc];
}


- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Thank you

dejo
Feb 3, 2012, 04:44 PM
Just a reminder: Posting code, please use the [CODE] tags (http://forums.macrumors.com/showthread.php?t=747660)