0

この質問を説明するのは難しいです。UIButtonsのエントリ数に基づいた一連の画面を作成しようとしていますNSArrayNSArray次のように、多数の画像を含む を作成しました。

socialArray = [[NSArray alloc] initWithObjects:
                   [UIImage imageNamed:@"FacebookButton.png"],
                   [UIImage imageNamed:@"WebsiteSocialButton.png"],
                   [UIImage imageNamed:@"TumblrButton.png"],
                   [UIImage imageNamed:@"TwitterButton.png"],
                   [UIImage imageNamed:@"InstagramButton.png"],
                   [UIImage imageNamed:@"VimeoButton.png"],
                   [UIImage imageNamed:@"GooglePlusButton.png"],
                   [UIImage imageNamed:@"YouTubeButton.png"],
                   [UIImage imageNamed:@"PinterestButton.png"],
                  nil];

それに基づいて、次のようにビュー内のアイテムの数を作成しています。

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [socialArray count];
}

次に、プログラムでボタンを作成し、そのイメージを次のように設定しています。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{ 
    UIButton *button = (UIButton *)view;
    if (button == nil)
    {
        //no button available to recycle, so create new one
        UIImage *image = [UIImage imageNamed:@"page.png"];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    }

    //set button label
    [button setTitle:[socialArray objectAtIndex:0] forState:UIControlStateNormal];

    return button;
}

私が本当にやりたいことは、配列内の指定された画像に基づいてボタンの画像を設定することです。したがって、最初のボタンはインデックス 0 で指定された PNG ファイルを取得し、2 番目のボタンはインデックス 1 で画像を取得する、というように続きます。

どうすればそれができるかについてのアドバイスはありますか?

4

4 に答える 4

1
UIImage *image = [socialArray objectAtIndex:index];

if(ステートメントの外でこれを行う必要があります。そうしないと、再利用されるボタンの画像が設定されません。)

于 2012-05-11T14:04:14.223 に答える
0

これを試して

UIButton *button = (UIButton *)view;
int i=0;
    if (button == nil)
    {
        //no button available to recycle, so create new one
        UIImage *image = [imgArray objectAtIndex:i];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        i++;

    }
于 2012-05-11T14:10:10.063 に答える
0

配列から取得します。

UIImage *image = [socialArray objectAtIndex:index];
于 2012-05-11T14:05:55.333 に答える
0

次のことはできません UIImage *image = [socialArray objectAtIndex:index];

index配列よりも大きくなる可能性がある場合は、次のように変更します。

UIImage *image = [socialArray objectAtIndex:(index % [self numberOfItemsInCarousel:carousel])];
于 2012-05-11T14:05:58.803 に答える