1 //完成布局设计 2 3 //三列 4 int totalloc=3; 5 // 宽度 6 CGFloat appvieww=85; 7 //高度 8 CGFloat appviewh=30; 9 10 CGFloat margin=(WIDTHW-totalloc*appvieww)/(totalloc+1); 11 CGFloat marginY=10; 12 for (int i=0; i<self.allarray.count; i++) { 13 int row=i/totalloc;//行号 14 //1/3=0,2/3=0,3/3=1; 15 int loc=i%totalloc;//列号 16 17 CGFloat appviewx=margin+(margin+appvieww)*loc; 18 CGFloat appviewy=marginY+(marginY+appviewh)*row; 19 20 //创建地区按钮 21 UIButton *regionBtn=[[UIButton alloc] initWithFrame:CGRectMake(appviewx, appviewy+64, appvieww, appviewh)]; 22 [self.view addSubview:regionBtn]; 23 //regionBtn.backgroundColor=[UIColor greenColor]; 24 NSLog(@"#####buttons###allarray###%@",self.allarray[i]); 25 regionBtn.titleLabel.font=[UIFont systemFontOfSize:12]; 26 RegionModel *regionModel=[[RegionModel alloc] init]; 27 regionModel=_allarray[i]; 28 [regionBtn setTitle:regionModel.name forState:UIControlStateNormal]; 29 [regionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 30 [regionBtn addTarget:self action:@selector(didClickRegion) forControlEvents:UIControlEventTouchUpInside]; 31 32 }