for循环创建UIbutton和UIimageview等控件

int totalColumns = 3;

//  每一格的尺寸

CGFloat cellW = buttonSpace;

CGFloat cellH = 35;

// 间隙

CGFloat margin =10;

//根据格子个数创建对应的框框

for(int index = 0; index < _dataArr.count; index++) {

_Moviebutton = [[UIButton alloc]init];

[_Moviebutton setBackgroundColor:NormalColor];

// 计算行号  和   列号

int row = index / totalColumns;

int col = index % totalColumns;

//根据行号和列号来确定 子控件的坐标

CGFloat cellX = margin + col * (cellW + margin);

CGFloat cellY = row * (cellH + margin);

_Moviebutton.frame = CGRectMake(cellX+15, cellY+170, cellW, cellH);

[_Moviebutton setTitle:[NSString stringWithFormat:@"%@",_dataArr[index]] forState:UIControlStateNormal];

_Moviebutton.titleLabel.font = FONT(14);

[_Moviebutton setTitleColor:kMainBlueColor forState:UIControlStateNormal];

_Moviebutton.tag = 100+index;

[_Moviebutton addTarget:self action:@selector(moveBtnAction:) forControlEvents:UIControlEventTouchUpInside];

// 添加到view 中

[self.view addSubview:_Moviebutton];

}

你可能感兴趣的:(for循环创建UIbutton和UIimageview等控件)