2013-08-31 45 views
-1

我在故事板中使用MultiCarousel,並且出現錯誤,無法在傳送帶上顯示圖像。是否存在與代碼中的問題?我從viewfromItemAtIndex得到錯誤說
預期的方法體
預期「:」
預期標識符或(
預期標識符或(
外來的大括號(「}」)
,如果你想看到一個特定部分,請隨時要求更多的代碼。圖片不顯示使用iCarousel

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

回答

1

你實際上是非常接近。你失蹤的方法開始的第一個支柱。在未來,Xcode的會將你指向一個位置a至少接近錯誤,您可以用它作爲分析代碼的起點。始終確保計算並平衡開放和關閉大括號,括號和父母。

- (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; 
} 
+0

謝謝錯誤已完全消失,但我無法看到圖片顯示在傳送帶上。我沒有任何錯誤,而且真的很奇怪,我看不到任何東西,只是我放在Carousel後面的UIimage。 – poradi12

+0

同樣的問題,你找到解決方案嗎? – XIMRX