I'm trying to create a simple pickerview with two components, drawing its label data from a small mutablearray and output data from a simple matrix. The purpose of this wheel is to select a value from 0 to 1000, and then spit out the number in a label and the value to the rest of the application's functions.
Some specific info about what I'm doing: (skip down to the last paragraph for the problem)
The left wheel spins a "hundred's" column, and the right wheel spins a "ten's" column, so essentially you're creating one value from two wheels. All I want to do is let the user create a value of 0, 10, 20, 30... 990, 1000 (every ten units up to 1000).The first component is easy to label, but I have two arrays to populate the second component's row's labels. The first array for the second component creates the 00 - 90 label, the second array (for when 1000 is selected) just has a 00 value. So when the user wants to select 1000, the 10 is on the first wheel and 00 on the second. I change the row count for the second wheel component when the first wheel component is at row 10. So now the second component only shows "00". My didSelectRow method uses a matrix for values of 0 - 990 and works great. And I just make a string and convert it to a number for when the user selects 1000 using if statements.
The problem is in the rare circumstance of when the user spins Component 0 to create the value of "1000" (the last row), and if they were start spinning the second component before the first component has a chance to stop spinning (basically spinning in a hurry!), the app crashes. I think it's trying to find a value for a row that doesn't exist. I have other parts of my app that function similarly and they also crash under the same situation. Is there a problem with my approach to changing the number of rows/labels for rows based on the selection of a different component? Or is it something simple in my code?
Thanks for reading and thanks in advance for any help!
Here's the code:
//Baggage Array
baggageHundredsArray = [[NSMutableArray alloc] init];
for (int i = 1; i <= 10; i++) {
NSString *myBagString = [NSString stringWithFormat:@"%d", i];
[baggageHundredsArray addObject:myBagString];
}
[baggageHundredsArray insertObject:@"- " atIndex:0];
baggageTensArray = [[NSMutableArray alloc] init];
for (int i = 10; i <= 90; i = i + 10) {
NSString *myBagString2 = [NSString stringWithFormat:@"%d lbs.", i];
[baggageTensArray addObject:myBagString2];
}
[baggageTensArray insertObject:@"00 lbs." atIndex:0];
baggageTensArray2 = [[NSMutableArray alloc] init];
[baggageTensArray2 insertObject:@"00 lbs." atIndex:0];
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if (thePickerView == baggagePicker) {
NSInteger numComps2 = 0;
switch (component)
{
case 0:
numComps2 = [baggageHundredsArray count];
break;
case 1:
if ([baggagePicker selectedRowInComponent:0] <= 9)
{
numComps2 = [baggageTensArray count];
}
else
{
numComps2 = [baggageTensArray2 count];
}
break;
}
return numComps2;
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
if (pickerView == baggagePicker) {
if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) { //newlabel
CGRect frame = CGRectMake(0.0, 0.0, 110, 32.0);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
pickerLabel.textAlignment = UITextAlignmentLeft;
pickerLabel.backgroundColor = [UIColor clearColor];
pickerLabel.font = [UIFont boldSystemFontOfSize:12];
}
pickerLabel.textColor = [UIColor blackColor];
switch (component)
{
case 0:
//CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
//pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentRight];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:23]];
[pickerLabel setTextColor:[UIColor blackColor]];
[pickerLabel setText:[baggageHundredsArray objectAtIndex:row]];
break;
case 1:
if ([baggagePicker selectedRowInComponent:0] <= 9) {
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:21]];
[pickerLabel setTextColor:[UIColor blackColor]];
[pickerLabel setText:[baggageTensArray objectAtIndex:row]];
}
else
{
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:21]];
[pickerLabel setTextColor:[UIColor blackColor]];
[pickerLabel setText:[baggageTensArray2 objectAtIndex:row]];
}
break; }
return pickerLabel;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (thePickerView == baggagePicker) {
[baggagePicker reloadAllComponents];
NSInteger hundredsWheel = [thePickerView selectedRowInComponent:0];
NSInteger tensWheel = [thePickerView selectedRowInComponent:1];
//lbs.
int column5 [10][10] = {
{0,10,20,30,40,50,60,70,80,90},
{100,110,120,130,140,150,160,170,180,190},
{200,210,220,230,240,250,260,270,280,290},
{300,310,320,330,340,350,360,370,380,390},
{400,410,420,430,440,450,460,470,480,490},
{500,510,520,530,540,550,560,570,580,590},
{600,610,620,630,640,650,660,670,680,690},
{700,710,720,730,740,750,760,770,780,790},
{800,810,820,830,840,850,860,870,880,890},
{900,910,920,930,940,950,960,970,980,990},
};
// Totals Label
if (hundredsWheel <= 9) {
myBaggageString = [NSString stringWithFormat:@"%i", (column5[hundredsWheel][tensWheel])];
baggageWeightLabel.text = myBaggageString;
baggageWeightInt = [myBaggageString intValue];
baggageWeightFloat = [myBaggageString floatValue];
baggageMomentFloat = baggageWeightFloat * 731.10;
[self calculateWeight];
paxViewBaggageWeightLabel.text = myBaggageString;
NSLog(@"value of myBaggageString is %@", myBaggageString);
[baggagePicker reloadAllComponents];
}
if (hundredsWheel == 10){
myBaggageString = [NSString stringWithFormat:@"1000"];//, [lastFuelValues objectAtIndex: [weightPicker selectedRowInComponent:1]]];
baggageWeightLabel.text = myBaggageString;
baggageWeightInt = [myBaggageString intValue];
baggageWeightFloat = [myBaggageString floatValue];
baggageMomentFloat = baggageWeightFloat * 731.10;
[self calculateWeight];
paxViewBaggageWeightLabel.text = myBaggageString;
[baggagePicker reloadAllComponents];
}