我想使用iCarousel在视图控制器中显示多个图像。
我在NSMutableArray中列出我的所有图像 foto ,
然后我在iCarousel方法中调用了数组foto viewForItemAtIndex 。
我已经按照一些教程但仍然无法正常工作。
请帮我。
这是我的.m代码
#import "TestController.h"
@interface TestController () <UIActionSheetDelegate>
@property (nonatomic, retain) NSMutableArray *foto;
@end
@implementation TestController
@synthesize carousel;
@synthesize foto;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
foto = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Seleb1.jpg"],
[UIImage imageNamed:@"Seleb2.jpg"],
[UIImage imageNamed:@"Seleb3.jpg"],
nil];
}
return self;
}
- (void)dealloc
{
carousel.delegate = nil;
carousel.dataSource = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
carousel.type = iCarouselTypeCoverFlow2;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [foto count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[foto objectAtIndex:index]]];
return view;
}
@end
有人能帮帮我吗?
我的代码出了什么问题?
更新代码
#import "TestController.h"
@interface TestController ()
@property (nonatomic, retain) NSArray *foto;
@end
@implementation TestController
@synthesize carousel;
@synthesize foto;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
foto = [NSArray arrayWithObjects:
@"Seleb1.jpg",
@"Seleb2.jpg",
@"Seleb3.jpg",
nil];
}
return self;
}
- (void)dealloc
{
carousel.delegate = nil;
carousel.dataSource = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
carousel.type = iCarouselTypeCoverFlow2;
[carousel reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [foto count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; // set the frame for your Image
UIImage *image = [UIImage imageNamed:[foto objectAtIndex:index]];
((UIImageView *)view).image = image;
return view;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
//customize carousel display
return value+0.1;
}
@end
答案 0 :(得分:1)
更改此行
view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[foto objectAtIndex:index]]];
到
原因:您已将数组定义为
[UIImage imageNamed:@"Seleb1.jpg"]
,因此您需要在此处直接调用
view = [[UIImageView alloc] initWithImage:[foto objectAtIndex:index]];
<强>选择-2 强>
创建一个类似
的数组 foto = [NSArray arrayWithObjects:
@"Seleb1.jpg",
@"Seleb2.jpg",
@"Seleb3.jpg",
nil];
并调用
view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[foto objectAtIndex:index]]];
如果您需要
使用此
- (void)viewDidLoad
{
[super viewDidLoad];
carousel.type = iCarouselTypeCoverFlow2;
[carousel reloadData];
}
更新回答
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; // set the frame for your Image
UIImage *image = [UIImage imageNamed:[foto objectAtIndex:index]];
((UIImageView *)view).image = image;
return view;
}
答案 1 :(得分:0)
@TomKnapen:我应该在哪里设置它?如果它在故事板上,我已经 已经设定好了。在TestController.h中,我也将它设置为my 代码在.h:TestController:UIViewController - Shasapo
我不确定你是如何在故事板中设置它的,但在代码中,你没有设置委托。这一行没有设置委托TestController : UIViewController <iCarouselDataSource,iCarouselDelegate>
,它的作用是它使你的控制器,在这种情况下是TestController,符合iCarouselDelegate协议。您需要做的是在viewDidLoad
中明确设置委托和数据源,例如,
carousel.delegate = self;
carousel.datasource = self;
2 - 首先尝试调试这些委托方法。从nimberOfItems方法开始,检查foto数组是否为零。然后在viewForItem方法中检查您返回的图像是否为nil。 因为你正在使用storyboard,你的initWithNibName方法没有被调用,这就是为什么你没有初始化数组,我假设。在我看来,你的numberofitems方法返回0.尝试将断点int放入此方法并查看,或者只是添加一个NSLog语句并检查foto数组是否为零
3 - 您没有在视图中添加iCarousel视图。
我以前使用过这个框架,所以我决定包含我实现的代码片段
#pragma mark -
#pragma mark iCarousel methods
- (void)iCarouselSetup {
self.view.backgroundColor = kAppColor;
self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, calendarBar.bottom + 10, self.view.width, self.view.height)];
[self.view addSubview:self.carousel];
self.carousel.delegate = self;
self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeCoverFlow;
self.carousel.scrollEnabled = NO;
[self.carousel scrollToItemAtIndex:2 animated:NO];
self.currentProjectsTableview = (UITableView*)[self.carousel currentItemView]; }
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return 5; }
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
self.projectsTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
self.projectsTableview.x = 0;
self.projectsTableview.y = calendarBar.bottom;
self.projectsTableview.width = [ASize screenWidth];
self.projectsTableview.height = [ASize screenHeight] - 180;
self.projectsTableview.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
self.projectsTableview.backgroundColor = [UIColor fromRGB:0xF8F8F8];
self.projectsTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
self.projectsTableview.dataSource = self;
self.projectsTableview.delegate = self;
self.projectsTableview.tag = tableTag++;
return self.projectsTableview; }
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
self.carousel.scrollEnabled = NO;
[AppDelegate getDelegate].deckController.panningMode = IIViewDeckFullViewPanning;
[UIView animateWithDuration:0.3 animations:^{
for (int i = 0; i < self.carousel.numberOfItems; i++)
{
[self.carousel itemViewAtIndex:i].transform = CGAffineTransformMakeScale( 1.1f, 1.1f);
}
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
for (int i = 0; i < self.carousel.numberOfItems; i++)
{
[self.carousel itemViewAtIndex:i].transform = CGAffineTransformMakeScale( 1.0f, 1.0f);
[self.carousel itemViewAtIndex:i].userInteractionEnabled = YES;
}
} ];
tableMinimized = NO;
}];
self.currentProjectsTableview = (UITableView*)[carousel itemViewAtIndex:index]; }
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel {
[navButton setTitle:self.projectDates[self.carousel.currentItemIndex] forState:UIControlStateNormal]; }
答案 2 :(得分:0)
我通过结合你所有的答案修复我的代码,谢谢你。这是我的新代码:
#import "TestController.h"
@interface TestController ()
@property (nonatomic, retain) NSArray *foto;
@end
@implementation TestController
@synthesize carousel;
@synthesize foto;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
carousel.delegate = nil;
carousel.dataSource = nil;
}
- (void)viewDidLoad
{
carousel.delegate = self;
carousel.dataSource = self;
foto = [NSArray arrayWithObjects:
@"Seleb1.jpg",
@"Seleb2.jpg",
@"Seleb3.jpg",
nil];
[super viewDidLoad];
carousel.type = iCarouselTypeCoverFlow2;
[carousel reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
NSLog(@"%lu",(unsigned long)foto.count);
return [foto count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; // set the frame for your Image
UIImage *image = [UIImage imageNamed:[foto objectAtIndex:index]];
((UIImageView *)view).image = image;
return view;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
//customize carousel display
return value+0.1;
}
@end
非常感谢你的回答,我真的很感激:)