如何在UIButton图像中使用延迟加载?

时间:2011-09-17 06:53:27

标签: iphone uiscrollview uibutton

我使用此逻辑在滚动视图中创建按钮。 `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;
    }

`

1 个答案:

答案 0 :(得分:1)

您可以在here

上找到延迟加载的民主代码

希望从这一个你可以得到懒惰加载的逻辑...