1
使用下面的代碼循環200個按鈕,並在該行滿了時將該行下移一個缺口。 林猜測我必須有更好的方式,因爲我的方式劑量工作。Xcode:更改循環按鈕的行
當第二行和第三行開始時,我只有一個按鈕。沒有錯誤只是在最後一行上彼此按鈕。
-(void)viewDidLoad {
int numba=0;
int x=-20;
int y=20;
for(int i = 1; i <= 200; ++i) {
numba ++;
if (numba <16) {
x =x+20;
} else if (numba >16 && numba <26){
x=-20;
x = x + 20;
y=40;
} else if (numba >26 && numba <36){
x=-20;
x =x+20;
y=60;
} else {
x=-20;
x =x+20;
y=80;
}
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(x, y, 20, 20);
NSLog(@"numba = %d",numba);
NSLog(@"x = %d",x);
btn.tag = numba;
[btn setTitle:[NSString stringWithFormat: @"%d", numba] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
}