【发布时间】:2011-09-17 06:53:26
【问题描述】:
我使用这个逻辑在滚动视图中创建按钮。 `int m=0; int j = [图像数据计数]/3; int s = [图像数据计数]%3; 如果(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;
}
`
【问题讨论】:
-
试着解释一下你想达到什么,“延迟加载按钮图像”不是很清楚。
-
在这种方法中,我创建了一个按钮并在按钮上设置图像。所有按钮创建和图像设置然后滚动视图显示我想在按钮上一一加载图像的数据
-
您好,我想在滚动视图中创建许多按钮,然后在该按钮上加载图像。
标签: iphone uiscrollview uibutton