我正在为我的应用程序(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'而终止应用,原因:'< strong> - [__ 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 :(得分: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;
}