2012-12-16 17 views
3

我正在實施iCarousel(使用coverflow類型)來顯示一些可在我的應用程序中使用的視頻的圖像。 (2 seperate carousels,同樣的問題)iOS - iCarousel僅使用可見視圖的索引

我使用numberOfItemsInCarousel(當前60)和viewForItemAtIndex的視頻數組來使用我的數組中的對象來構建視圖。

但是,只有5個視頻正在使用。所以有60件物品出現在旋轉木馬中,但它只是重複了5件物品。當我在'viewForItemAtIndex'中記錄索引時,它只被調用5次(0,1,... 5是日誌結果)。 經過一些測試後,似乎唯一被調用的索引默認情況下是輪播中可見的。

什麼給? *陣列已經過測試,以確保它們填充所有獨特的視頻。該數組很好,它與iCarousel方法有關。

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{ 
    if(carousel==freeCarousel){ 
     NSLog(@"Free: %i",freeVideos.count); 
     return freeVideos.count; 
    } 
    if(carousel==feeCarousel){ 
     NSLog(@"Fee: %i",feeVideos.count); 
     return feeVideos.count; 
    } 
    return 0; 
} 
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{ 
    NSLog(@"TESTING"); 
    if (view == nil){ 
     //Copy array for easier use of global Video/File object 
     NSArray *copy = [[NSArray alloc] init]; 
     if(carousel==freeCarousel){ 
      copy = [NSArray arrayWithArray:freeVideos]; 
     } 
     else if(carousel==feeCarousel){ 
      copy = [NSArray arrayWithArray:feeVideos]; 
     } 

     Videos *currentVideo = [copy objectAtIndex:index]; 
     Files *file = [currentVideo valueForKey:@"files"]; 
     NSLog(@"TITLE TEST: %@",currentVideo.title); 
     //Video Button 
     UIButton *buttonVideo = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 465, 310)]; 
     [buttonVideo setTag:[currentVideo.videoID intValue]]; 
     [buttonVideo addTarget:self action:@selector(showVideoDetails:) forControlEvents:UIControlEventTouchUpInside]; 

     //Thumbnail 
     UIImageView *videoThumbnail = [[UIImageView alloc] initWithFrame:buttonVideo.bounds]; 
     //Border 
     [videoThumbnail setBackgroundColor:[UIColor whiteColor]]; 

     //Caption 
     UIImage *captionImage = [UIImage imageNamed:@"video-box-caption"]; 
     UILabel *caption = [[UILabel alloc] initWithFrame:CGRectMake(0, videoThumbnail.frame.size.height-captionImage.size.height, videoThumbnail.frame.size.width, captionImage.size.height)]; 
     [caption setBackgroundColor:[UIColor colorWithPatternImage:captionImage]]; 
     [caption setTextAlignment:NSTextAlignmentCenter]; 
     [caption setFont:[UIFont fontWithName:@"Open Sans Condensed" size:16]]; 
     [caption setTextColor:[UIColor whiteColor]]; 
     [caption setText:currentVideo.title]; 
     [videoThumbnail addSubview:caption]; 

     if(file.thumbnailPath!=(id)[NSNull null] || ![file.thumbnailPath isEqualToString:@"missing_image.png"]){ 
      UIImage *thumbnailImage = [[DataManager sharedManager] generateThumbnailImage:currentVideo]; 
      [videoThumbnail setImage:thumbnailImage]; 
      [buttonVideo addSubview:videoThumbnail]; 
     }else{ 
      UIImage *thumbnailImage = [UIImage imageNamed:@"video-details-thumbnail"]; 
      [videoThumbnail setImage:thumbnailImage]; 
      [buttonVideo addSubview:videoThumbnail]; 
     } 
     view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonVideo.frame.size.width, buttonVideo.frame.size.height)]; 
     [view addSubview:buttonVideo]; 
    } 
    return view; 
} 

回答

4

您已經實現了viewForItemAtIndex方法錯誤。

iCarousel回收視圖以避免在移動時分配新對象(如UITableView)。 if(view == nil)檢查是說「如果view是nil創建一個新視圖,否則使用我已經設置的視圖而不修改它)

移動任何索引特定的邏輯if(如果查看== nil)檢查,如果你看看庫中包含的iCarousel例子,它會顯示如何正確地做到這一點。

+0

啊,好吧,這是我的想法實際上,只是還沒有測試過。看看並回復你。謝謝! – JimmyJammed

+0

是的,它解決了它。謝謝! – JimmyJammed