0

这个问题很难解释。我正在尝试创建一组UIButtons基于NSArray. 我创建了一个NSArray包含许多图像的图像,如下所示:

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 文件,第二个按钮获取索引 1 处的图像,依此类推。

关于我如何做到这一点的任何建议?

4

4 回答 4

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

(您必须在语句之外执行此操作if,否则不会为重复使用的按钮设置图像。)

于 2012-05-11T14:04:14.223 回答
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 回答
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 回答