【问题标题】:iCarousel Mixed elements problemsiCarousel 混合元素问题
【发布时间】:2014-02-03 19:45:50
【问题描述】:

我在使用带有轮子样式的 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 并不总是同一张照片,我不知道为什么...

有什么帮助吗?

谢谢!

【问题讨论】:

    标签: ios icarousel


    【解决方案1】:

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

    这与您在使用表格视图、UICollectionViews 或任何使用单元回收的 API 时将面临的问题相同。

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

    【讨论】:

    • 明白!感谢您的帮助!这里是解决方案,在 viewForItemAtIndex: 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;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2021-03-19
    相关资源
    最近更新 更多