2012-11-15 27 views
0

我正在爲我的應用(iGear)添加一個散點圖,因此當用戶選擇與自行車上的cogset結合使用的一個,兩個或三個鏈環時,線條將顯示齒輪儀表。核心劇情:只適用於三塊地塊

問題是Core Plot只顯示三個鏈環被選中時的情節。我需要你的幫助,這是我在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超出界限爲空數組」 第一擲調用堆棧: (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:終止調用拋出異常

謝謝!

+0

'cassette.numCogs'和'cassetteNumCogs'的值是什麼? –

+0

根據選定的cogset,它可以是8,9,10或11。 「記錄」在子視圖中獲取此值。由於您可以選擇一個,兩個或三個鏈環(因此可以有一到三個圖),我將在「繪製」圖之前檢查第二個和第三個數組是否爲空。 – Luis

回答

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