为什么我的多组件 UIPickerView 崩溃了?

发布于 2024-09-11 14:36:36 字数 7798 浏览 3 评论 0原文

我正在尝试创建一个具有两个组件的简单选取器视图,从一个小的可变数组中绘制其标签数据并从一个简单的矩阵中输出数据。该轮的目的是选择 0 到 1000 之间的值,然后将标签中的数字和值吐出到应用程序的其余功能。

关于我正在做的事情的一些具体信息:(跳到问题的最后一段)

左轮旋转“百”列,右轮旋转“十”列,所以本质上您正在创建一个值从两个轮子。我想做的就是让用户创建一个值 0, 10, 20, 30...990, 1000(每十个单位最多 1000)。第一个组件很容易标记,但我有两个数组要填充第二个组件的行标签。第二个组件的第一个数组创建 00 - 90 标签,第二个数组(选择 1000 时)仅具有 00 值。因此,当用户想要选择 1000 时,10 位于第一个轮子上,00 位于第二个轮子上。当第一个轮子组件位于第 10 行时,我更改了第二个轮子组件的行计数。所以现在第二个组件仅显示“00”。我的 didSelectRow 方法使用 0 - 990 值的矩阵并且效果很好。我只是创建一个字符串,并在用户使用 if 语句选择 1000 时将其转换为数字。

问题出在极少数情况下,当用户旋转组件 0 以创建值“1000”(最后一行)时,如果他们在第一个组件有机会停止旋转之前开始旋转第二个组件(基本上是旋转)快点!),应用程序崩溃了。我认为它试图为不存在的行找到一个值。我的应用程序的其他部分的功能类似,它们也会在相同的情况下崩溃。我根据不同组件的选择来更改行/行标签数的方法是否存在问题?或者我的代码中的东西很简单?

感谢您的阅读并提前感谢您的帮助!

这是代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文