iOS for循环创建九宫格

2018-05-17  本文已影响100人  隔墙送来秋千影

for循环创建九宫格时,注意某个item是在第几行和第几列、
上下间距自定义、

- (void) setupButtonWithArr:(NSArray *)array {
    CGFloat btnW = (SCREEN_WIDTH - 40 *SCALE_WIDTH)/3;
    CGFloat btnH = btnW *1.35;
    for (int i = 0; i < array.count; i ++) {
        NSInteger row = i/3;
        NSInteger col = i%3;
        CGFloat btnX = 10 *SCALE_WIDTH + (btnW + 10 *SCALE_WIDTH) * col;
        CGFloat btnY = 10 *SCALE_WIDTH + (btnH + 50 *SCALE_WIDTH) * row;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(btnX,btnY, btnW, btnH);
        [btn sd_setImageWithURL:[NSURL URLWithString:array[i][@"imgUrl"]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [self.view addSubview:btn];
    }
}
上一篇下一篇

猜你喜欢

热点阅读