2012-03-10 56 views
0

目前,我指的是this post和我能夠創建一個2 * 2的網格視圖編程創建iphone 2 * 2和2 * 1網格視圖

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個視圖。

有沒有簡單的解決方案可用於此?

+0

通過調試器的步驟並檢查每個變量中的每個值。你應該能夠發現問題所在。 – TigerCoding 2012-03-10 21:56:04

+0

我發現我的解決方案通過以下這個:http://stackoverflow.com/questions/9782499/creating-a-new-row-of-uibutton-programatically/9782670#9782670 – user1227928 2012-03-20 07:28:45

回答

0

你所描述的是預期的行爲。一列中的兩行恰好是兩個單元格或其表示視圖。

+0

所以我想在第一行創建2個視圖和下面一行的單個視圖,我該怎麼做? – user1227928 2012-03-10 22:03:33

+0

如果您想使用此代碼,請將其調用兩次。一旦'rows = 2; cols = 1;'和一次'rows = 1; cols = 1;'。您可以調整初始偏移量以獲得理想的結果。 – Mundi 2012-03-10 22:11:13

+0

是否有任何簡單的解決方案替代上述代碼可用? – user1227928 2012-03-10 22:14:02