iCarousel 不显示我的图片 objective C

iCarousel doesn't show my images objective C

我想使用 iCarousel 在视图控制器中显示多个图像。

我在 NSMutableArray 中列出了我所有的图像 foto,
然后我在 iCarousel 方法中调用数组照片 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

更改此行

 view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[foto objectAtIndex:index]]];

Reason : you have already defined your array as [UIImage imageNamed:@"Seleb1.jpg"] , so in here you need call directly like

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;

}

@TomKnapen : where should i set it ? if it's on the story board, i've set it already. in the TestController.h, i've set it too here's my code in .h : TestController : UIViewController – Shasapo

我不确定你是如何在故事板中设置它的,但在代码中,你没有设置委托。此行没有设置委托 TestController : UIViewController <iCarouselDataSource,iCarouselDelegate>,它的作用是使您的控制器(在本例中为 TestController)遵守 iCarouselDelegate 协议。您需要做的是在 viewDidLoad 中显式设置委托和数据源,例如

carousel.delegate = self;

carousel.datasource = self;

2 - 首先尝试调试那些委托方法。从 nimberOfItems 方法开始,检查 foto 数组是否为 nil。然后在 viewForItem 方法中检查您 return 的图像是否不为零。 因为您使用的是故事板,所以您的 initWithNibName 方法没有被调用,这就是为什么您的照片数组没有被初始化,我假设。在我看来,您的 numberofitems 方法 returns 0。尝试在此方法中放置断点并查看,或者仅添加 NSLog 语句并检查 foto 数组是否为 nil

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]; }

我通过结合您的所有答案来修复我的代码,非常感谢。这是我的新代码:

#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

非常感谢您的所有回答,我真的很感激:)