Pregunta

I'm having a problem using iCarousel with wheel style, because i really need that elements appears sorted in the view and in my case, elements are always mixed by the component.

Here is my code:

#import "ProductsViewController.h"

@interface ProductsViewController ()

@property (readonly, strong, nonatomic) NSArray *items;

@end

@implementation ProductsViewController

- (void)awakeFromNib
{
    _items = @[@"Prod1",
                   @"Prod2",
                   @"Prod3",
                   @"Prod4",
                   @"Prod5",
                   @"Prod6",
                   @"Prod7",
                   @"Prod8",
                   @"Prod9"];
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    //configure carousel
    _carousel.type = iCarouselTypeWheel;
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    //free up memory by releasing subviews
    _carousel = nil;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel
    return [_items count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;

    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 360.0f, 360.0f)];
        ((UIImageView *)view).image = [UIImage imageNamed: [_items objectAtIndex:index]];
        view.contentMode = UIViewContentModeCenter;
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 360.0f, 360.0f)];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [label.font fontWithSize:50];
        label.tag = 1;
        [view addSubview:label];
    }
    else
    {
        //get a reference to the label in the recycled view
        label = (UILabel *)[view viewWithTag:1];
    }

    label.text = [NSString stringWithFormat:@"%d", index];

    return view;
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)objcarousel{

    NSLog(@" carouselCurrentItemIndexDidChange == %d", objcarousel.currentItemIndex);
}


@end

At carouselCurrentItemIndexDidChange:, objcarousel.currentItemIndex are not always the same photo, i don't know why...

Any help, please?

Thanks!

¿Fue útil?

Solución

Your code is only installing an image into the image view if view == nil. If you get passed a recycled view then you leave the image alone. That means your tile with still have the last image that was used, which will very likely be the wrong image.

This is the same issue that you will face with table views, UICollectionViews, or just about any API the uses cell recycling like this.

You need to move the code that installs an image into your image view outside of the if (view == nil) ... else block and down with the code that sets the label text.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top