3

我正在实现 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;
}
4

1 回答 1

4

您错误地实现了 viewForItemAtIndex 方法。

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

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

于 2012-12-16T08:58:16.983 回答