这个示例代码让我定义了一些东西,但我还不想呢?

时间:2011-05-07 03:59:30

标签: ios objective-c c-preprocessor

我有一些开源代码,包括:

·H:

#define TILE_ROWS    6
#define TILE_COLUMNS 2
#define TILE_COUNT   (TILE_ROWS * TILE_COLUMNS)

@class Tile;

@interface TilesViewController : UIViewController {
@private
    CGRect   tileFrame[TILE_COUNT];
    Tile    *tileForFrame[TILE_COUNT];

}

然后整个.m,就像这样:

   for (int row = 0; row < TILE_ROWS; ++row) {
        for (int col = 0; col < TILE_COLUMNS; ++col) {

tileFrame[index] = frame;

 tileForFrame[index] = tile;

但我想要的是能够将TILE_ROWS设置为结果,例如:

float rowsNeeded = ceil(rowsNeededA/TILE_COLUMNS);

因此,它需要稍后,但我认为CGRect和Tile只能在那里定义。我需要帮助,我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

您必须将定义变为实例变量并动态分配数组:

@class Tile;

@interface TilesViewController : UIViewController {
@private
    int       tileRows;
    int       tileColumns;
    int       tileCount;
    CGRect*   tileFrame;
    Tile**    tileForFrame;
}

...

tileColumns = 2;
tileRows = (rowsNeededA + tileColumns - 1) / tileColumns;
tileCount = tileColumns * tileRows;
tileFrame = (CGRect*)malloc(tileCount * sizeof(CGRect));
tileForFrame = (Tile**)malloc(tileCount * sizeof(Tile*));

for循环变为:

for (int row = 0; row < tileCount; ++row) {
    for (int col = 0; col < tileCount; ++col) {