根据输入的个数,打印每行做多输出3个的for循环

#define Screen_width  [[UIScreen mainScreen] bounds].size.width

 

- (void)viewDidLoad {

    [superviewDidLoad];

    //要显示的个数

    intHow = 11;

//    要显示的列数

    intlie = 4;

//    中间的间隔

    intjiange = 10;

    //每行最多3个,根据个数看需要显示几行

    introw;

    if(How%lie>0) {

        row = How/lie+1;

        

    }else{

        row = How/lie;

    }

    //   根据输入的个数打印一个3列的数据    外控制行  内控制列

    intxZhou ;

    intyZhou ;

    for(inti = 0; i < row; i++) {

        yZhou = (Screen_width-(lie+1)*jiange)/lie*i+i*jiange+jiange;

        

        for(inty = 0; y< lie; y++){

            xZhou = (Screen_width-(lie+1)*jiange)/lie*y+y*jiange+jiange;

            

            if(i*lie+y+1 == How){

                break;

            }else{

                

                printf("x轴:%d   y轴:%d",xZhou,yZhou);

                UIButton*addPhotoButton = [[UIButtonalloc]initWithFrame:CGRectMake(xZhou, yZhou, (Screen_width-(lie+1)*jiange)/lie, (Screen_width-(lie+1)*jiange)/lie)];

                addPhotoButton.backgroundColor= [UIColorgreenColor];

                [self.viewaddSubview:addPhotoButton];

                printf("*");

            }

            

        }

        printf("\n");

    }

    

    // Do any additional setup after loading the view, typically from a nib.

}

转载于:https://www.cnblogs.com/AlienY/p/4676887.html