0

アプリ (iGear) に散布図を追加しているので、ユーザーが自転車のコグセットと組み合わせて 1 つ、2 つ、または 3 つのチェーンリングを選択すると、ギア メーターが線で表示されます。

問題は、Core Plot が 3 つのチェーンリングが選択されている場合にのみプロットを表示することです。私はあなたの助けが必要です.これはCore Plotでの私の最初の試みであり、私は迷っています.

私のコードは次のとおりです。

iGearMainViewController.m

- (IBAction)showScatterIpad:(id)sender {
cogsetToPass = [NSMutableArray new];
arrayForChainringOne = [NSMutableArray new];
arrayForChainringTwo = [NSMutableArray new];
arrayForChainringThree = [NSMutableArray new];
//behavior according to number of chainrings
switch (self.segmentedControl.selectedSegmentIndex) {

    case 0: // one chainring selected

        for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
            if (i <10) {
                corona = [NSString stringWithFormat:@"cog0%d",i];

            }else {
                corona = [NSString stringWithFormat:@"cog%d",i];
            }
            float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
            float teeth = [[cassette valueForKey:corona] floatValue];
            [cogsetToPass addObject:[NSNumber numberWithFloat:teeth]];
            [arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
        }
            break;
     case 1: // two chainrings selected

        for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
            if (i <10) {
                corona = [NSString stringWithFormat:@"cog0%d",i];

            }else {
                corona = [NSString stringWithFormat:@"cog%d",i];
            }
            float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
            //NSLog(@" gearsForOneChainring = %@",[NSNumber numberWithFloat:one]);
            float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
            [cogsetToPass addObject:[NSNumber numberWithFloat:[[cassette valueForKey:corona]floatValue]]];
            [arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
            [arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
        }
        break;
    case 2: // three chainrings selected
        for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
            if (i <10) {
                corona = [NSString stringWithFormat:@"cog0%d",i];

            }else {
                corona = [NSString stringWithFormat:@"cog%d",i];
            }
            float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
            float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
            float three = (wheelSize*[_threeChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
            [cogsetToPass addObject:[cassette valueForKey:corona]];
            [arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
            [arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
            [arrayForChainringThree addObject:[NSNumber numberWithFloat:three]];

        }
    default:
        break;
}

ScatterIpadViewController *sivc = [[ScatterIpadViewController alloc]initWithNibName: @"ScatterIpadViewController" bundle:nil];
[sivc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    sivc.records = [cassetteNumCogs integerValue];
    sivc.cogsetSelected = self.cogsetToPass;
    sivc.chainringOne = self.arrayForChainringOne;
    sivc.chainringThree = self.arrayForChainringThree;
    sivc.chainringTwo = self.arrayForChainringTwo;
    [self presentViewController:sivc animated:YES completion:nil];

}

そして、プロットを描画するコードを含む子ビュー:

ScatterIpadViewController.m

    #pragma mark - CPTPlotDataSource methods

- (NSUInteger)numberOfRecordsForPlot: (CPTPlot *)plot {
    return records;
}

- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
    switch (fieldEnum)
    {
        case CPTScatterPlotFieldX:
            return [NSNumber numberWithInt:index];
        break;

    case CPTScatterPlotFieldY:{

        if ([plot.identifier isEqual:@"one"]==YES) {
            //NSLog(@"chainringOne objectAtIndex:index = %@", [chainringOne objectAtIndex:index]);
            return [chainringOne objectAtIndex:index];
        }else if ([plot.identifier isEqual:@"two"] == YES ){
            //NSLog(@"chainringTwo objectAtIndex:index = %@", [chainringTwo objectAtIndex:index]);
            return [chainringTwo objectAtIndex:index];

        }else if ([plot.identifier isEqual:@"three"] == YES){
            //NSLog(@"chainringThree objectAtIndex:index = %@", [chainringThree objectAtIndex:index]);
            return  [chainringThree objectAtIndex:index];

        }

    default:
        break;
        }
  }
  return nil;
}

返されるエラーは、空の配列にアクセスしようとした場合の例外です。

2012-11-15 11:02:42.962 iGearScatter[3283:11603]キャッチされていない例外 'NSRangeException' が原因でアプリを終了しています。理由: '-[__NSArrayM objectAtIndex:]: 空の配列の範囲を超えたインデックス 0' First throw call stack: (0x1989012 0x1696e7e 0x192b0b4 0x166cd 0x183f4 0x1bd39 0x179c0 0x194fb 0x199e1 0x43250 0x14b66 0x13ef0 0x13e89 0x3b5753 0x3b5b2f 0x3b5d54 0x3c35c9 0x5c0814 0x392594 0x39221c 0x394563 0x3103b6 0x310554 0x1e87d8 0x27b3014 0x27a37d5 0x192faf5 0x192ef44 0x192ee1b 0x29ea7e3 0x29ea668 0x2d265c 0x22dd 0x2205 0x1) * libc++abi.dylib:例外をスローして呼び出された終了

ありがとうございました!

4

1 に答える 1

0

解決しました!自転車に乗っているときに光を見ました。

- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{

    if (([chainringOne count]!=0) && ([chainringTwo count]==0) && ([chainringThree count]==0)) {
        switch (fieldEnum) {
            case CPTScatterPlotFieldX:
                 return [NSNumber numberWithInt:(index+1)];
                break;

            case CPTScatterPlotFieldY:{
                if ([plot.identifier isEqual:@"one"]) {
                    return [chainringOne objectAtIndex:index];
                }
            }
            default:
            break;
        }
    }else if (([chainringOne count]!=0) && ([chainringTwo count]!=0) && ([chainringThree count]==0)){
        switch (fieldEnum) {
            case CPTScatterPlotFieldX:
                return [NSNumber numberWithInt:(index+1)];
                break;
            case CPTScatterPlotFieldY:{
                if ([plot.identifier isEqual:@"one"]) {
                     return [chainringOne objectAtIndex:index];
                }else if ([plot.identifier isEqual:@"two"]){
                    return [chainringTwo objectAtIndex:index];
                }
            }
            default:
            break;
        }
    }else if (([chainringOne count]!=0) && ([chainringTwo count]!=0) && ([chainringThree count]!= 0)){
        switch (fieldEnum) {
            case CPTScatterPlotFieldX:
                return [NSNumber numberWithInt:(index+1)];
                break;
            case CPTScatterPlotFieldY:{
                if (([chainringOne count] !=0) && ([plot.identifier isEqual:@"one"])) {
                    return [chainringOne objectAtIndex:index];
        }else if ((chainringTwo || chainringTwo.count) &&([plot.identifier isEqual:@"two"] == YES )){
              return [chainringTwo objectAtIndex:index];

         }else if ((chainringThree || chainringTwo.count)&& ([plot.identifier isEqual:@"three"] == YES)){
                return  [chainringThree objectAtIndex:index];
            }
        default:
            break;
            }
        }
    }
    return nil;
}
于 2012-11-16T22:57:44.383 に答える