在NSMutableArray中存储动态数据的问题?

时间:2010-04-05 10:58:17

标签: iphone

我想开发一个应用程序,首先我开发了一个用于存储X轴和y轴的结构代码。

struct TCo_ordinates {     浮动x;     漂浮y; }; 。 然后在drawRect方法中生成一个像。的结构对象。

struct TCo_ordinates *tCoordianates; 

现在我绘制Y轴的图形,其代码为。

    fltX1 = 30;
fltY1 = 5;
fltX2 = fltX1;
fltY2 = 270;
CGContextMoveToPoint(ctx, fltX1, fltY1);
CGContextAddLineToPoint(ctx, fltX2, fltY2);
NSArray *hoursInDays = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];
for(int intIndex = 0  ; intIndex < [hoursInDays count] ; fltY2-=20, intIndex++)
{   
    CGContextSetRGBStrokeColor(ctx, 2, 2, 2, 1); 
    //CGContextSetRGBStrokeColor(ctx, 1.0f/255.0f, 1.0f/255.0f, 1.0f/255.0f, 1.0f);   
    CGContextMoveToPoint(ctx, fltX1-3 , fltY2-40);
    CGContextAddLineToPoint(ctx, fltX1+3, fltY2-40);
    CGContextSelectFont(ctx, "Helvetica", 14.0, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(ctx, kCGTextFill);
    CGContextSetRGBFillColor(ctx, 0, 255, 255, 1);
    CGAffineTransform xform = CGAffineTransformMake(
                                                    1.0,  0.0,
                                                    0.0, -1.0,
                                                    0.0,  0.0);
    CGContextSetTextMatrix(ctx, xform);
    const char *arrayDataForYAxis = [[hoursInDays objectAtIndex:intIndex] UTF8String];
    float x1 = fltX1-23;
    float y1 = fltY2-37;
    CGContextShowTextAtPoint(ctx, x1, y1, arrayDataForYAxis, strlen(arrayDataForYAxis));
    CGContextStrokePath(ctx);

现在我想动态地在NSMutableArray中存储生成x1和y1的值,因为我编写了代码。

NSMutableArray *yAxisCoordinates = [[NSMutableArray alloc] autorelease];
    for(int yObject = 0; yObject < intIndex; yObject++)
    {
        [yAxisCoordinates insertObject:(tCoordianates->x = x1,tCoordianates->y = y1) atIndex:yObject];  

    }

但它没有奏效。我如何在yAxisCoordinates对象中存储x1和y1值。 上面的代码是正确的?????????????

1 个答案:

答案 0 :(得分:2)

据我所见(我没有阅读整个绘图代码,只是最后一部分),您的代码存在一些问题:

  1. 您正在创建一个新的点结构,您只需使用CGPoint
  2. 您未在init上致电NSMutableArray
  3. 您正在尝试将某些内容插入到数组中,但它不是TCo_ordinates结构。
  4. 即使它是,你也不能NSArray中存储结构,你必须存储一个对象。
  5. 以下是在NSArray中存储点的一种方法:

    NSMutableArray *foo = [NSMutableArray array];
    NSValue * bar = [NSValue valueWithCGPoint:CGPointMake(x, y)];
    [foo addObject:bar];
    

    您可以稍后通过以下方式检索您的观点:

    CGPoint point = [[foo objectAtIndex:i] CGPointValue];