iCarousel混合元素问题

时间:2014-02-03 19:45:50

标签: ios icarousel

我在使用带轮样式的 iCarousel 时遇到问题,因为我真的需要在视图中排序元素,在我的情况下,元素总是被组件混合。

这是我的代码:

#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

carouselCurrentItemIndexDidChange:,objcarousel.currentItemIndex并不总是相同的照片,我不知道为什么......

请帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果view == nil,您的代码只会将图片安装到图片视图中。如果您通过了回收的视图,那么您将保留图像。这意味着您的磁贴仍然具有最后使用的图像,这很可能是错误的图像。

这与您将使用表视图,UICollectionViews或任何使用此类单元格回收的API相同的问题。

您需要将安装图像的代码移动到if (view == nil) ... else块之外的图像视图中,然后使用设置标签文本的代码向下移动。