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

。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只能在此处定义。我需要帮助,我不确定该怎么办。

最佳答案

您必须将define转换为实例变量并动态分配数组:

@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) {

07-28 09:41