obj c“for loop”路障

时间:2012-01-30 05:52:41

标签: iphone objective-c for-loop

我在一个简单的条形图中显示了一些数据,这些条形图显示了一些值,但一切正常,但是现在要为我的Y轴构建比例,我对一些基本的东西有一些问题,

在这种情况下,我使用scaleMaxInt得到了我的图表的上限值,用于测试= 900

然后根据一些简单的测试,我得到例如9个分[加零] ......

所以为了使事情有效,我创建了int pp = 100

- (void) drawScaleLabels:(int)scaleMaxInt 
{

     //temp division for scale, NOTE WHERE TO USE 9 DIVISIONS!!

     int scaleStep = scaleMaxInt/9;
     NSLog(@"va ::%d", scaleStep);

     // case scaleMax : 0 < scaleMax < 1000
     float scaleDiv = 31.5;

     for (int i = 0; i<9; i++) 
     { // is 8, to 9 only for testing!!
         int pp = 100;

         self.divScaleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(469, scaleDiv+286, 60, 14)]autorelease];  
         self.divScaleLabel.font = [UIFont fontWithName:@"FS Albert" size:14];
         self.divScaleLabel.textColor = [UIColor whiteColor];
         self.divScaleLabel.textAlignment = UITextAlignmentRight;
         self.divScaleLabel.text =[NSString stringWithFormat:@"$%d",scaleMaxInt-scaleStep];

         self.divScaleLabel.backgroundColor =[UIColor clearColor];
         [self.view addSubview:self.divScaleLabel];

         scaleDiv = scaleDiv + 31.5;
         scaleStep = scaleStep+pp;
     }

}

显示Y轴的值列表:900,800,700,600 ... 0

但如果我使用int scaleStep = scaleMaxInt/9

的结果

它给了我列表,但值为* 2

- (void) drawScaleLabels:(int)scaleMaxInt 
{
    //temp division for scale, NOTE WHERE TO USE 9 DIVISIONS!!

    int scaleStep = scaleMaxInt/9;

    NSLog(@"va ::%d", scaleStep);

    // case scaleMax : 0 < scaleMax < 1000
    float scaleDiv = 31.5;

    for (int i = 0; i<9; i++) 
    { // is 8, to 9 only for testing!!

        //int pp = 100;

        self.divScaleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(469, scaleDiv+286, 60, 14)]autorelease];  
        self.divScaleLabel.font = [UIFont fontWithName:@"FS Albert" size:14];
        self.divScaleLabel.textColor = [UIColor whiteColor];
        self.divScaleLabel.textAlignment = UITextAlignmentRight;
        self.divScaleLabel.text =[NSString stringWithFormat:@"$%d",scaleMaxInt-scaleStep];

        self.divScaleLabel.backgroundColor =[UIColor clearColor];
        [self.view addSubview:self.divScaleLabel];

        scaleDiv = scaleDiv + 31.5;
        NSLog(@"va ::%d", scaleStep);
        scaleStep = scaleStep+scaleStep;
   }

}

所以Y轴是,900,800,700,500,100,-700 ...... - 24700

我愚蠢地坚持这个!,

如何生成Y轴列表?,动态值取决于scaleMaxInt 那不会混淆??

非常感谢!

1 个答案:

答案 0 :(得分:1)

你在循环中有这个:

scaleStep = scaleStep+scaleStep;

这意味着你在每次循环中加倍scaleStep。删除该行。相反,请设置self.divScaleLabel.text,如下所示:

self.divScaleLabel.text =[NSString stringWithFormat:@"$%d", scaleMaxInt - scaleStep * i];