2012-05-15 105 views
0

內我有這樣的代碼:環路另一個循環

//Value of userCount is 25 
auxCount = 0; 
for (int y_axis=0; y_axis<=8; y_axis++) //ROWS 
    { 
     for (int x_axis=0; x_axis<=2; x_axis++) //COLUMNS 
     { 
      if (auxCount<userCount) {  
       NSLog(@"auxCount: %i\n",auxCount); 
       NSLog(@"userCount: %i\n\n",userCount); 
       UIButton *btn=  [[UIButton alloc] initWithFrame:CGRectMake(16+100*x_axis,115.0*y_axis,88.0 ,88.0)]; 
       UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(16+100*x_axis,90+115.0*y_axis, 88.0, 15.0)]; 
       userLabel.textAlignment = UITextAlignmentCenter; 
       userLabel.text = mensaje; 

       btn.backgroundColor=[UIColor groupTableViewBackgroundColor]; 
       [scrollViewUsers addSubview:btn]; 
       [scrollViewUsers addSubview:userLabel]; 

       auxCount++; 

      } 
     } 
    } 

有了這個我想和3列和X行的矩陣,但僅顯示3行和第三行只顯示1個按鈕。和在調試出現面積: auxCount:0 USERCOUNT:25

auxCount:4 USERCOUNT:25

auxCount:8 USERCOUNT:25

auxCount:12 USERCOUNT:25

auxCount:16 userCount:25

auxCount:20 USERCOUNT位:25

auxCount:24 USERCOUNT位:25個

auxCount遞增4×4​​,我認爲這是因爲,如果指令僅由第一個for循環執行,但我不知道爲什麼。 請,我需要你的幫助。

ps:抱歉我的英語!

+0

似乎在這裏工作得很好。你確定你沒有在其他地方改變一些價值嗎? – Alexander

+0

是的,我確定。 auxCount和userCount僅在此代碼中使用 –

回答

1

只是嘗試這個循環,因爲我在我的應用程序中使用它爲我工作。

找出行數取決於列數和總數

int r; 
float rem = [dao libraryCount] % kCol; 
if(rem == 0.0f) 
    r = floor([dao libraryCount]/kD); 
else 
    r = ceil([dao libraryCount]/kD); 

這裏r是號碼,如果行[dao libraryCount是項目和kCol的總數爲列的固定號碼,你這是圖3和kD是相同kCol只尊重是它是浮動即3.0的類型在你的情況

然後使用for循環如下

for (int row = 0; row < r; ++row) 
{ 
    for (int col = 0; col < kCol; ++col) 
    { 
     //Your Code to display or any thing 
    } 
} 

auxCount使用此代碼

int index = (row * kCol) + col; 
     if(index < [dao libraryCount]) 
     { 
      //Your Code to display or any thing 
     } 

把這個邊都for循環,而不是使用++ 只是改變了兩個for環路並與相應的變量

享受編碼:) 更換if條件祝你好運

任何幫助是nee德德只是評論我我很樂意幫助你

+0

我不確定我的理解,但我會嘗試實現它 –

+0

看到我的更新的答案,這將幫助您更多 –

相關問題