我在我的应用程序(iGear)中添加了一个散点图,所以当用户选择一个、两个或三个链环与自行车上的一个齿轮组相结合时,线将显示齿轮表。
问题是,核心图仅在选择三个链环时才显示这些图。我需要你的帮助,这是我在核心情节的第一次尝试,我迷路了。
我的代码如下:
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-15 11:02:42.962 iGearScatter3283:11603 iGearScatter3283:11603终止应用程序
谢谢!
发布于 2012-11-16 22:57:44
解决了!我骑自行车时看到了灯。
- (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;
}https://stackoverflow.com/questions/13395210
复制相似问题