1.定义一个变量itemcount=3;//用于展示3列图片框
2.for循环遍历for (NSInteger index = 0; index < self.appes.count; index++)创建视图UIImageView,其列数为:index % 3;行数为index /3; 然后创建UIImageView对象。
4.将其加到父 视图上即可。
主要代码如下:
CGFloat viewX = 0;
CGFloat viewY = 40;
CGFloat viewW = 120;
CGFloat viewH = 140;
CGFloat margin = 12;
CGFloat screenView = self.view.bounds.size.width;
CGFloat leftMargin = (screenView - (itemCount * viewW) - (itemCount - 1) * margin) * 0.5;
for (NSInteger i = 0 ; i < 10; i++) {
NSInteger col = i % itemCount;//控制列
NSInteger row = i / itemCount;//控制行
CGFloat appX = leftMargin + (viewW + margin) * col;
CGFloat appY = viewY + (viewH + margin) * row;
UIImageView *appView =[ [UIImageView alloc]init];
appView.frame = CGRectMake(appX, appY, viewW, viewH);
[self.view addSubview:appView]; |
|