注释要添加的按钮的总个数为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;
}