2013-02-11 56 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

相關問題