【问题标题】:picture does not show up using iCarousel使用 iCarousel 不显示图片
【发布时间】:2013-09-03 19:55:24
【问题描述】:

我在情节提要中使用 MultiCarousel,但出现错误,无法在轮播中显示图像。代码有问题吗?我从 viewfromItemAtIndex 收到错误说
预期的方法体
预计':'
预期标识符或 (
预期标识符或 (
多余的右大括号 ("}")
如果您想查看特定部分,请随时索取更多代码。

#import "iCarouselExampleViewController.h"


@interface iCarouselExampleViewController ()

@property (nonatomic, retain) NSMutableArray *items1;
@property (nonatomic, retain) NSMutableArray *items2;

@end


@implementation iCarouselExampleViewController

@synthesize carousel1;
@synthesize carousel2;
@synthesize items1;
@synthesize items2;
- (void)awakeFromNib
{
    //set up data sources
    self.items1 = [NSMutableArray array];
    for (int i = 0; i < 100; i++)
    {
        [items1 addObject:[NSNumber numberWithInt:i]];
    }

    self.items2 = [NSMutableArray array];
    for (int i = 65; i < 65 + 58; i++)
    {
        [items2 addObject:[NSString stringWithFormat:@"%c", i]];
    }
}

- (void)dealloc
{
    //it's a good idea to set these to nil here to avoid
    //sending messages to a deallocated viewcontroller
    carousel1.delegate = self;
    carousel1.dataSource = self;
    carousel2.delegate = self;
    carousel2.dataSource = self;


}

#pragma mark -
#pragma mark View lifecycle

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager  = [NSFileManager defaultManager];

    //configure carousel1
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:@"Tops"];
    NSArray *directoryContent = [fileManager directoryContentsAtPath: fPath];
    imageArray1 = [directoryContent mutableCopy];

    //configure carousel2
    fPath = [documentsDirectory stringByAppendingPathComponent:@"Bottoms"];
    directoryContent = [fileManager directoryContentsAtPath:fPath];
    imageArray2 = [directoryContent mutableCopy];

    carousel1.type = iCarouselTypeLinear;
    carousel2.type = iCarouselTypeLinear;
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    //free up memory by releasing subviews
    self.carousel1 = nil;
    self.carousel2 = nil;
}

#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel
    if (carousel == carousel1)
    {
        return [imageArray1 count];
    }
    else
    {
        return [imageArray2 count];
    }
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view

if (view == nil)
{
    view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];

    UIImage *image;
    if (carousel == carousel1)
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
    else
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
    return view;
}
else {
    UIImage *image;
    if (carousel == carousel1)
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
    else
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
}


return view;
}

【问题讨论】:

    标签: ios directory icarousel


    【解决方案1】:

    你真的很亲密。您在方法开始时缺少第一个大括号。将来,Xcode 将指向至少靠近错误的位置,您可以将其用作分析代码的起点。始终确保计算和平衡左大括号、括号和括号。

    - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
    { // <--- This brace was missing!!
        if (view == nil)
        {
            view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
    
            UIImage *image;
            if (carousel == carousel1)
            {
                image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
                ((UIImageView *)view).image = image;
            }
            else
            {
                image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
                ((UIImageView *)view).image = image;
            }
            return view;
        }
        else {
            UIImage *image;
            if (carousel == carousel1)
            {
                image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
                ((UIImageView *)view).image = image;
            }
            else
            {
                image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
                ((UIImageView *)view).image = image;
            }
        }
        return view;
    }
    

    【讨论】:

    • 谢谢 错误完全消失了,但是我看不到图片出现在轮播上。我没有任何错误,很奇怪我看不到任何东西,只有我放在 Carousel 后面的 UIimage。
    • 同样的问题,你找到解决办法了吗?
    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2023-03-28
    • 2017-12-21
    • 1970-01-01
    • 2014-10-19
    相关资源
    最近更新 更多