如何重新加载iCarousel图像?

时间:2013-05-16 05:19:12

标签: iphone ios6

如何在不同的uibutton动作中将不同的图像数组传递给iCarousel。如何用不同的图像重新加载iCarousel?

先谢谢。

2 个答案:

答案 0 :(得分:5)

方法- (void)reloadData; UITableView

相同

以下是GitHub / iCarousel的描述,

  

这将从dataSource重新加载所有轮播视图并刷新轮播显示。

答案 1 :(得分:1)

使用iCarousel上的iCarouselButtonsDemo项目尝试此操作:

iCarouselExampleViewController.h文件中的

#import <UIKit/UIKit.h>
#import "iCarousel.h"


@interface iCarouselExampleViewController : UIViewController <iCarouselDataSource, iCarouselDelegate> {
    NSMutableArray *arrImages;
}

@property (nonatomic, retain) IBOutlet iCarousel *carousel;
@property (nonatomic, retain) NSMutableArray *arrImages;

@end
iCarouselExampleViewController.m文件中的

#import "iCarouselExampleViewController.h"

@implementation iCarouselExampleViewController

@synthesize carousel,arrImages;

- (void)dealloc {
    carousel.delegate = nil;
    carousel.dataSource = nil;
    [carousel release];
    [arrImages release];
    [super dealloc];
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad     {
    [super viewDidLoad];

    arrImages = [[NSMutableArray alloc] init];

    for (int i = 0; i < 100; i++) {
        [arrImages addObject:[NSString stringWithFormat:@"Image%d.png",i]];
    }
    //configure carousel
    carousel.type = iCarouselTypeCoverFlow2;
}    

- (void)viewDidUnload {
    [super viewDidUnload];
    self.carousel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    //generate 100 buttons
    //normally we'd use a backing array
    //as shown in the basic iOS example
    //but for this example we haven't bothered
    return 100;
}

- (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:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:[arrImages objectAtIndex:index]] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:[arrImages objectAtIndex:index]] forState:UIControlStateNormal];
return button;
}

#pragma mark -
#pragma mark Button tap event

- (void)buttonTapped:(UIButton *)sender {
//get item index for button
NSInteger index = [carousel indexOfItemViewOrSubview:sender];

    [[[[UIAlertView alloc] initWithTitle:@"Button Tapped"
                             message:[NSString stringWithFormat:@"You tapped button number %i", index]
                            delegate:nil
                   cancelButtonTitle:@"OK"
                   otherButtonTitles:nil] autorelease] show];
}

@end