【发布时间】: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 并不总是同一张照片,我不知道为什么...
有什么帮助吗?
谢谢!
【问题讨论】: