开发者

This sample code has me define something, but I don't want to just yet?

I have some open source code, which includes this:

.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];

}

And then throughout the .m, like so:

   for (int 开发者_运维问答row = 0; row < TILE_ROWS; ++row) {
        for (int col = 0; col < TILE_COLUMNS; ++col) {

and

tileFrame[index] = frame;

and

 tileForFrame[index] = tile;

But what i want is to be able to set TILE_ROWS to the outcome of, for example:

float rowsNeeded = ceil(rowsNeededA/TILE_COLUMNS);

So therefore it would need to be later on, but I think the CGRect and Tile can only be defined there. I need help, I'm not sure what to do.


You have to turn the defines into instance variable and to allocate the arrays dynamically:

@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*));

The for loop then becomes:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜