1

我想创建一个将动态创建的按钮的网格视图。单击网格视图中的按钮为每个按钮执行不同的任务。我该怎么做?任何帮助将不胜感激?

4

4 回答 4

1

以下代码可以插入viewDidLoad到您的视图控制器中,并假设您已经在界面构建器中设置了 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

这可能会帮助您 PTSpringBoard 网格菜单检测示例代码

https://github.com/ppanopticon/PTSSpringboard

在此处输入图像描述

于 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 回答