我使用此逻辑在滚动视图中创建按钮。 `int m = 0; int j = [imageData count] / 3; int s = [imageData count]%3; if(s == 1 || s == 2) { 当J = J + 1; } scrollView.contentSize = CGSizeMake(320,j * 155);
int i,k,x=0,y=0;
for(i=0;i<j;i++)
{
if(s==0){
for(k=0;k<3;k++)
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(13+x,10+y, 91,135)];
int p;
NSLog(@"%i",p++);
[button setTag:m];
[button setBackgroundColor:[UIColor whiteColor]];
NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:m]]];
[button setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
x=x+101;
m++;
}
}
else if(s==1)
{
for(k=0;k<3;k++)
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(13+x,10+y, 91,135)];
[button setTitle:@"Button" forState:UIControlStateNormal];
int p;
NSLog(@"%i",p++);
[button setTag:m];
[button setBackgroundColor:[UIColor whiteColor]];
NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:m]]];
[button setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
x=x+101;
if(i==j-1 && k==0)
{
break;
}
m++;
}
}
else if(s==2)
{
for(k=0;k<3;k++)
{
button = [UIButton buttonWithType:UIButtonTypeCustom];;
[button setFrame:CGRectMake(13+x,10+y, 91,135)];
[button setTitle:@"Button" forState:UIControlStateNormal];
int p;
NSLog(@"%i",p++);
[button setTag:m];
[button setBackgroundColor:[UIColor whiteColor]];
NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:m]]];
[button setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
x=x+101;
if(i==j-1 && k==1)
{
break;
}
m++;
}
}
x=0;
y=y+145;
}
`