我有一些开源代码,包括:
·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只能在那里定义。我需要帮助,我不知道该怎么做。
答案 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) {