動的に作成されるボタンのグリッドビューを作成したいのですが、グリッドビューでボタンをクリックすると、ボタンごとに異なるタスクが実行されます。これを行うにはどうすればよいですか。
質問する
1246 次
4 に答える
1
次のコードはviewDidLoad
、View Controller 内に挿入できます。インターフェイス ビルダーで UISCrollView をセットアップしていると仮定します。それ以外の場合は、スクロール ビューを 内に割り当てる必要がありますviewDidLoad
。
このコードは、可変数の画像項目を 2 列の UIScrollView に追加します。
UIButton * button = nil;
//The x and y view location coordinates for your menu items
int x = 0, y = 0;
//The number of images you want
int numOfItemsToAdd = 10;
//The height and width of your images are the screen width devided by the number of columns
int imageHeight = 320/4, imageWidth = 320/4;
int numberOfColumns = 2;
//The content seize needs to refelect the number of items that will be added
[scrollView setContentSize:CGSizeMake(320, imageHeight*numOfItemsToAdd];
for(int i=0; i<numOfItemsToAdd; i++){
if(i%numberOfColumns == 0){//% the number of columns you want
x = 0;
if(i!=0)
y += imageHeight;
}else{
x = imageHeight;
}
button = [[UIButton alloc] initWithFrame: CGRectMake(x, y, imageWidth, imageHeight)];
//set the center of the image in the scrollviews coordinate system
imageView.center = CGPointMake(x, y);
//Finaly add the image to the scorll view
[scrollView addSubview:button];
そして、各タグが各ボタンに割り当てられ、各ボタンにイベントを与えるように、ボタンにタグをインクリメンタルに与えることにより、グリッドビューで異なるタスクを与えます。以下で
-(void) buttonClicked:(UIButton*)mybutton{ }
于 2012-11-07T07:55:16.360 に答える
1
This may help you PTSpringBoard sample codefor grid menu detection
于 2012-11-07T08:31:22.403 に答える
0
スクロールビューを使用する前にこれを行いましたこれが役立つ場合があります。
int number_of_types = 20; // (no of total buttons )
int btncount=0;
int number_of_buttons_in_row = 4;
CGFloat screen_width = 280.0, button_width = 70;
CGFloat xpadd = (screen_width - ( number_of_buttons_in_row * button_width ))/(number_of_buttons_in_row+1);
CGFloat x_coord, y_ccord;
int buttonXOffset, buttonYOffset, buttonWidth, buttonHeight;
buttonWidth = 57;
buttonHeight = 57;
x_coord = 20;
y_ccord = 70;
int number_of_rows;
if((number_of_types % number_of_buttons_in_row) == 0)
number_of_rows = number_of_types / number_of_buttons_in_row;
else
number_of_rows = number_of_types / number_of_buttons_in_row + 1;
int number_of_buttons_count = 0;
for (UIView * view in scrollBack.subviews) {
if(view.tag!=999 )
{
[view removeFromSuperview];
view = nil;
}
}
for (int i = 0; i< number_of_rows; i++) {
for (int j=0; j<number_of_buttons_in_row; j++) {
buttonXOffset = 75;
buttonYOffset = 58;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(40, 0, 57, 57);
x_coord += button_width/2 + xpadd;
btn.center = CGPointMake(x_coord, y_ccord);
[scrollBack addSubview:btn];
buttonXOffset = x_coord - buttonWidth/2;
buttonYOffset = y_ccord - buttonHeight/2;
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(buttonXOffset, buttonYOffset + buttonHeight + 4, buttonWidth+7, 28)];
btn.tag= btncount;
[btn setTitle:@"your Title "];
//if titles are in a Array then you can Use
[btn setTitle:[titleArray objectatindex:btncount] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(yourFunction:) forControlEvents:UIControlEventTouchUpInside];
label.text = [titleArray objectatindex:btncount];
label.tag = 777;
label.backgroundColor = [UIColor clearColor];
label.font=[UIFont fontWithName:@"Helvetica" size:11];
label.font=[UIFont boldSystemFontOfSize:11];
label.textAlignment = UITextAlignmentCenter;
label.numberOfLines = 0;
[scrollBack addSubview:label];
[label release];
x_coord += button_width/2;
number_of_buttons_count++;
if(number_of_buttons_count == 20)
break;
btncount++;
}
if(number_of_buttons_count == 20)
break;
y_ccord += 120;
x_coord = 20;
}
scrollBack.contentSize = CGSizeMake(320, buttonYOffset + 200);
}
于 2012-11-08T07:57:30.850 に答える
-1
for (int i=0; i<totalButtons; i++){
UIButton* abutton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
[abutton setTitle:@"Button_Title" forState:UIControlStateNormal];
abutton.tag=100+i;
[abutton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[gridView addSubview:abutton];
//here you need to calculate and set the position for the button
abutton.center=CGPointMake(calculated_X, calculated_Y);
[abutton release];
}
-(void) buttonClicked:(UIButton*)mybutton{
NSLog(@"%d is the Tag",mybutton.tag);
//here you do what you want with the indexed button clickes
}
于 2012-11-07T08:06:27.083 に答える