2010-04-05 48 views
1

我想開發一個應用程序,首先我開發了一個用於存儲X軸和Y軸的結構代碼。將動態數據存儲在NSMutableArray中的問題?

struct TCo_ordinates {0} 0 0 0 float x; float 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); 

現在我想存儲動態生成的X1和Y1的值NSMutableArray裏,對於我寫的代碼。

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值。 以上代碼是正確的?????????????

回答

2

至於我可以看到(我沒看過整個繪圖代碼,剛剛過去的部分),也有你的代碼的幾個問題:

  1. 你正在創建一個新的起點結構在那裏你可以使用CGPoint
  2. 您在NSMutableArray上不打電話init
  3. 你試圖插入東西到數組中,但它不是TCo_ordinates結構。
  4. 即使是這樣,你的也不能NSArray中存儲一個結構,你必須存儲一個對象。

這裏有一種方法,以點存儲在一個NSArray

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

,你可以在以後檢索你的觀點:

CGPoint point = [[foo objectAtIndex:i] CGPointValue]; 
相關問題