私のプロジェクトには、Web サービスからの値を持つラベルと画像を持つ UIButtons を作成するこのコードがあります。
- (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 を作成したコードは次のとおりです。
- (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];
}
私のすべての UIButtons はそれぞれのタグで作成されており、それらのボタンのそれぞれに画像を配置したいと考えています。上記のコードは、最後に作成された UIButton でのみ機能します。上記のコードを使用して、すべてのボタンに画像を入力したいと考えています。どこが間違っているのか誰か教えてもらえますか?