2013-02-11 53 views
0

我在我的项目中创建了带有标签和图像以及来自Web服务的值的UIButton的代码。使用AFNetworking设置UIButton图像

- (void)getMoviePosterImages 
{ 
    for (int i = 0; i < [self.rowCount intValue]; i++) 
    { 
     NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i]; 
     AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]] imageProcessingBlock:nil 
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
     { 
      [moviePosterButton setBackgroundImage:image forState:UIControlStateNormal]; 
     } 
     failure:nil]; 

     [operation start]; 
    } 
} 

这里就是我创建的UILabels代码:我所有的UIButtons与它们各自的标记创建

- (void)displayMovieTitlesAndFrames 
{ 
    int imageCount = [self.rowCount intValue]; 
    int offset_x = 21; 
    int offset_y = 24; 

    for (int i = 0; i < imageCount; i++) 
    { 
     // compute x & y offset 
     if (i % 3 == 0) 
     { 
      offset_x = 21; 
     } 
     else if (i % 3 == 1) 
     { 
      offset_x = 115; 
     } 
     else 
     { 
      offset_x = 210; 
     } 

     whiteBackgroundLabel = [[[UILabel alloc] initWithFrame:CGRectMake(offset_x, offset_y, MOVIE_THUMBNAIL_W, MOVIE_THUMBNAIL_H)] autorelease]; 
     whiteBackgroundLabel.backgroundColor = [UIColor whiteColor]; 
     whiteBackgroundLabel.layer.cornerRadius = 3.0; 
     whiteBackgroundLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor]; 
     whiteBackgroundLabel.layer.borderWidth = 1.0; 
     [scrollview addSubview:whiteBackgroundLabel]; 

     moviePosterButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
     moviePosterButton.frame = CGRectMake(offset_x + 5, offset_y + 5, MOVIE_THUMBNAIL_W - 10, MOVIE_THUMBNAIL_H - 10); 
     moviePosterButton.backgroundColor = [UIColor clearColor]; 
     [moviePosterButton setBackgroundImage:[UIImage imageNamed:@"placeholder.png"] forState:UIControlStateNormal]; 
     moviePosterButton.tag = i; 
     [moviePosterButton addTarget:self 
           action:@selector(imageButtonPressed:) 
        forControlEvents:UIControlEventTouchUpInside]; 
     [scrollview addSubview:moviePosterButton]; 

     movieTitleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(offset_x, offset_y + 143, 90, 20)] autorelease]; 
     movieTitleLabel.backgroundColor = [UIColor whiteColor]; 
     movieTitleLabel.layer.cornerRadius = 3.0; 
     movieTitleLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor]; 
     movieTitleLabel.layer.borderWidth = 1.0; 
     movieTitleLabel.font = [UIFont fontWithName:@"Helvetica" size:11.0]; 
     movieTitleLabel.textColor = [UIColor darkGrayColor]; 
     movieTitleLabel.textAlignment = UITextAlignmentCenter; 
     movieTitleLabel.numberOfLines = 1; 
     movieTitleLabel.text = [[self.jsonMutableArray objectAtIndex:i] objectAtIndex:1]; 
     [scrollview addSubview:movieTitleLabel]; 

     quickBuyButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
     quickBuyButton.frame = CGRectMake(offset_x + 1 + (MOVIE_THUMBNAIL_W - QUICK_BUY_BUTTON_W - 2), offset_y - 1 + 2, QUICK_BUY_BUTTON_W, QUICK_BUY_BUTTON_H); 
     quickBuyButton.backgroundColor = [UIColor clearColor]; 
     [quickBuyButton setBackgroundImage:QUICK_BUY_BUTTON forState:UIControlStateNormal]; 
     quickBuyButton.tag = i; 
     [quickBuyButton addTarget:self 
          action:@selector(quickBuyButtonPressed:) 
       forControlEvents:UIControlEventTouchUpInside]; 
     [scrollview addSubview:quickBuyButton]; 

     // increment offset 
     if (offset_x == 210) 
     { 
      offset_y += 171; 
     } 
    } 

    if ((offset_x == 21) || (offset_x == 115)) 
    { 
     offset_y += 171; 
    } 

    [self adjustScrollViewAndBackground:offset_y]; 
} 

,我希望把他们的图像上的每个上这些按钮。上面的代码只适用于最后创建的UIButton。我想用上面的代码填充所有按钮的图像。有人可以告诉我我在哪里弄错了吗?

回答

0

在您的getMoviePosterImages方法中,您正在将图像设置为moviePosterButton,这可能是您刚创建的最后一个按钮,因此如果只有最后一个按钮获取图像,则这是正常的。您需要通过标签检索每个按钮,或者使用所有按钮构建数组,以便轻松访问它们。也许你可以做这样的事情:

- (void)getMoviePosterImages 
{ 
    for (int i = 0; i < [self.rowCount intValue]; i++) 
    { 
     NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i]; 
     AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]] imageProcessingBlock:nil 
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
     { 
      [(UIButton *)buttonsArray[i] setBackgroundImage:image forState:UIControlStateNormal]; 
      // or 
      // [(UIButton *)[self.view viewWithTag:i] setBackgroundImage:image forState:UIControlStateNormal]; 
     } 
     failure:nil]; 

     [operation start]; 
    } 
} 

希望这有助于;)

+0

我的按钮是滚动视图内。我怎样才能实现它? 'self.scrollView'不起作用。 – jaytrixz 2013-02-11 09:03:23

+0

只需使用'scrollview'。我也注意到moviePosterButton.tag和quickBuyButton.tag具有相同的标记,这不是好的 – kaal101 2013-02-11 09:14:34

+0

没关系。 quickBuyButton只是一个通用图像。 – jaytrixz 2013-02-12 01:33:42

相关问题