以编程方式在iphone中创建2 * 2和2 * 1网格视图

时间:2012-03-10 21:41:48

标签: iphone gridview

目前我指的是this post,我可以创建一个2 * 2网格视图

int rows = 2;
int cols = 2;

float gridWidth = 1024.0;
float gridHeight = 1024.0;

float buttonWidth = 100.0;
float buttonHeight = 100.0;

// float gapHorizontal = (gridWidth - (buttonWidth * rows)) / (rows + 1);
// float gapVertical = (gridHeight - (buttonHeight * cols)) / (cols + 1);

float gapHorizontal = 40;
float gapVertical = 40;

float offsetX;
float offsetY;

int count = 0;

do {
    offsetX = gapHorizontal + ((count % rows) * (buttonWidth + gapHorizontal));
    offsetY = gapVertical + ((count / rows) * (buttonHeight + gapVertical));

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(offsetX, offsetY,  buttonWidth, buttonHeight)];
    view.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:view];


    offsetX+= buttonWidth + gapHorizontal;

    count++;

} while(count < rows * cols);

但是当我尝试创建一个2 * 1网格视图时

将此更改为

int rows = 2;
int cols = 1;

我无法这样做。它只创造了2个视图。

有没有可用的简单解决方案?

1 个答案:

答案 0 :(得分:0)

您描述的是预期的行为。具有一列的两行恰好是两个单元格或其表示视图。