iOS实现row行column列个按钮的添加

注释要添加的按钮的总个数为total 
-(void)addScrollAndButton{

    

    int  COLUMN=4;//列数

    int total = self.listArray.count;

    //int rows = (total / COLUMN) + ((total % COLUMN) > 0 ? 1 : 0);

    

    int btnwidth=50;

    int btnheight=30;

    int widthblank=20;

    int heightblank=20;

 

    for (int i=0; i<total; i++) {

        int row = i / COLUMN;//行数

        int column = i % COLUMN;//列数

        

        //添加按钮

        UIButton *aButton=[UIButton buttonWithType:UIButtonTypeCustom];

        aButton.frame=CGRectMake((widthblank+btnwidth)*column, (heightblank+btnheight)*row, btnwidth, btnheight);

        aButton.tag=1000+i;

        [aButton setTitle:[[[self.listArray objectAtIndex:i]objectForKey:@"Table"]objectForKey:@"Title"] forState:UIControlStateNormal];

        [aButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        [aButton setShowsTouchWhenHighlighted:YES];//按的时候设置高亮

        [aButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:aButton];

    }

 

}

-(void)buttonPressed:(id)sender

{

    UIButton * button = (UIButton *)sender;

    int index=button.tag-1000;

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。