iOS - iCarousel仅使用可见视图的索引

时间:2012-12-16 04:48:08

标签: ios view indexing icarousel

我正在实施iCarousel(使用coverflow类型)来显示我的应用中可用的一些视频的一堆图像。 (2个单独的转盘,同样的问题)

我使用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;
}

1 个答案:

答案 0 :(得分:4)

您已经实现了viewForItemAtIndex方法错误。

iCarousel回收视图以避免在移动时分配新对象(如UITableView)。 if(view == nil)检查说“如果view为nil,则创建一个新视图,否则使用我已设置的视图而不修改它。”

将任何特定于索引的逻辑移到if(if view == nil)检查之外。如果您查看库中包含的iCarousel示例,它会显示如何正确执行此操作。