Search code examples
iosviewindexingicarousel

iOS - iCarousel only uses indexes of visible views


I am implementing iCarousel (using coverflow type) to show a bunch of images of some videos that are available in my app. (2 seperate carousels, same problem)

I use the arrays of videos for the numberOfItemsInCarousel (currently 60) and viewForItemAtIndex to use the objects from my array to build out the view.

However, only 5 of the videos are being used. So 60 items are showing up in the carousel, but its just the same 5 items being repeated. When I logged the index inside of the 'viewForItemAtIndex', it only gets called 5 times (0,1,...5 are the log results). After some testing, it seems that the only indexes being called are for whatever is visible in the carousel by default.

What gives? *The arrays have been tested to ensure they are populated with all unique videos. The array is fine, its something to do with the iCarousel method.

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

Solution

  • You've implemented the viewForItemAtIndex method wrong.

    iCarousel recycles views to avoid allocating new objects while moving (like a UITableView). The if (view == nil) check is saying "if view is nil create a new view, otherwise use a view I've already set without modifying it).

    Move any index-specific logic outside of the if (if view == nil) check. If you look at the iCarousel examples included with the library, it shows how to do this correctly.