Reputation: 1
I am trying to create an app using UIPickerView with three components. The first component has 3 rows. The second component has 6 rows and the third component has 12 rows. Every time I scroll beyond row 3 in components 2 or 3 the application crashes and points to the first components array. I'm sure this is easy to fix but I'm lost. Thanks in advance.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
{
NSString *resultString0 = [[NSString alloc] initWithFormat:
@"Sensor: %@",
[modelArray objectAtIndex:row]];
modelLabel.text = resultString0;
[resultString0 release];
}
{
NSString *resultString1 = [[NSString alloc] initWithFormat:
@"Pixels: %@",
[memoryArray objectAtIndex:row]];
memoryLabel.text = resultString1;
[resultString1 release];
NSString *firstString = horizFaceRecLabel.text;
float horizontalFace = [[horizontalArray objectAtIndex:row] floatValue];
float requiredFace = 100;
output1 = [firstString floatValue];
output1 = horizontalFace / requiredFace;
horizFaceRecLabel.text = [NSString stringWithFormat:(@"%.1f ft"), output1];
NSString *secondString = horizLicensePlateLabel.text;
float horizontalLicense = [[horizontalArray objectAtIndex:row] floatValue];
float requiredLicense = 45;
output2 = [secondString floatValue];
output2 = horizontalLicense / requiredLicense;
horizLicensePlateLabel.text = [NSString stringWithFormat:(@"%.1f ft"), output2];
NSString *thirdString = horizVisualIdLabel.text;
float horizontalVisual = [[horizontalArray objectAtIndex:row] floatValue];
float requiredVisual = 30;
output3 = [thirdString floatValue];
output3 = horizontalVisual / requiredVisual;
horizVisualIdLabel.text = [NSString stringWithFormat:(@"%.1f ft"), output3];
}
{
NSString *resultString2 = [[NSString alloc] initWithFormat:
@"Lens: %@",
[lensArray objectAtIndex:row]];
lensLabel.text = resultString2;
[resultString2 release];
{
NSString *firstDistString = faceRecLabel.text;
float angle = [[anglesArrayA objectAtIndex:row] floatValue];
float densityOne;
float densityTwo;
densityOne = [firstDistString floatValue];
densityTwo = (output1/2)/(sin(angle/2));
faceRecLabel.text = [NSString stringWithFormat:(@"%.1f ft"), densityTwo];
}
}
}
Upvotes: 0
Views: 213
Reputation:
It looks like you're missing some if
statements to control when each block of code gets called.
You have three separate blocks of code presumably for each component but since there's no condition to control the flow, all the blocks get executed whenever a row in any component is selected. (The third block also has another block inside it--don't know why you've written it like that.)
When you go beyond row 3 in the 2nd and 3rd components, the code for the 1st component runs and crashes when it tries to get the 4th row in the array for component 1 (which doesn't exist).
Since the component indexes are zero-based, the conditions should be something like:
if (component == 0)
{
//code for 1st component here...
}
else if (component == 1)
{
//code for 2nd component here...
}
else if (component == 2)
{
//code for 3rd component here...
}
You could also use a switch
statement instead.
Upvotes: 1