3

私はiCarouselを(カバーフロータイプを使用して)実装して、アプリで利用できるいくつかのビデオの画像の束を表示しています。(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 に答える