/**
* 创建方块
*/
- (void)createSquares:(NSArray *)sqaures
{
// 一行最多4列
int maxCols = 4;
// 宽度和高度
CGFloat buttonW = XMGScreenW / maxCols;
CGFloat buttonH = buttonW;
for (int i = 0; i<sqaures.count; i++) {
// 创建按钮
XMGSqaureButton *button = [XMGSqaureButton buttonWithType:UIButtonTypeCustom];
// 监听点击
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
// 传递模型
button.square = sqaures[i];
[self addSubview:button];
// 计算frame
int col = i % maxCols;
int row = i / maxCols;
button.x = col * buttonW;
button.y = row * buttonH;
button.width = buttonW;
button.height = buttonH;
}
// 8个方块, 每行显示4个, 计算行数 8/4 == 2 2
// 9个方块, 每行显示4个, 计算行数 9/4 == 2 3
// 7个方块, 每行显示4个, 计算行数 7/4 == 1 2
// 总行数
// NSUInteger rows = sqaures.count / maxCols;
// if (sqaures.count % maxCols) { // 不能整除, + 1
// rows++;
// }
// 总页数 == (总个数 + 每页的最大数 - 1) / 每页最大数
NSUInteger rows = (sqaures.count + maxCols - 1) / maxCols;
// 计算footer的高度
self.height = rows * buttonH;
}
网友评论