2

https://github.com/nicklockwood/iCarousel/issues/278

iCarousel を使用して画像を直線的に表示します。これらの画像は動的に取得されます。2枚の画像があるとします。しかし、iCarousel を更新すると、次の画像のように背景に 2 つの画像も表示されます。


コードのデバッグ中に、 reloadData関数を呼び出した後、viewForItemAtIndex 関数が 2 回呼び出されていることがわかりました。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view



したがって、iCarousel には 4 つの画像があります。

2回呼ばれないようにするには?

この問題をできるだけ早く解決するのを手伝ってください。


- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UIImageView *puzzlePhoto = nil;
    UIImageView *puzzleBG = nil;
    UILabel *label = nil;

    if (view == nil)
    {
        view = [[UIImageView alloc] init];
        view.frame = CGRectMake(0, 0, 128, 112);
        view.backgroundColor = [UIColor clearColor];
        view.layer.doubleSided = NO;
        puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];

        //! get friend's photo
        FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
        UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];

        if (imgPuzzle)
            puzzlePhoto.image = imgPuzzle;
        else
            puzzlePhoto.image = [UIImage imageNamed:@"puzzle background_small.png"];

        puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
        if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) ||
            ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
            puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg_me.png"];
        else
            puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg.png"];

        //! get friend name
        FUser *userFriend = foresightCurrent.m_userFriend;
        label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
        label.text = userFriend.m_strName;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:10];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];

        [view addSubview:puzzlePhoto];
        [view addSubview:puzzleBG];
        [view addSubview:label];
    }

    return view;
}
4

2 に答える 2

2

この問題は、ほとんどの場合、ビューが正しくリサイクルされていないことが原因です。ビューは異なるインデックスで再利用されるため、ビュー作成コードでインデックス固有のプロパティを設定しないでください。アイテム ビューを設定する正しい方法は次のとおりです。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UIImageView *puzzlePhoto = nil;
    UIImageView *puzzleBG = nil;
    UILabel *label = nil;

    const NSInteger puzzlePhotoTag = 1;
    const NSInteger puzzleBGTag = 2;
    const NSInteger labelTag = 3;

    if (view == nil)
    {
        //*************************************************
        //do setup that is the same for every item view here
        //*************************************************

        view = [[UIImageView alloc] init];
        view.frame = CGRectMake(0, 0, 128, 112);
        view.backgroundColor = [UIColor clearColor];
        view.layer.doubleSided = NO;

        puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
        puzzlePhoto.tag = puzzlePhotoTag;
        [view addSubview:puzzlePhoto];

        puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
        puzzleBG.tag = puzzleBGTag;
        [view addSubview:puzzleBG];

        label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:10];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
        label.tag = labelTag;
        [view addSubview:label];
    }
    else
    {
        //get references to subviews
        puzzlePhoto = (UIImageView *)[view viewWithTag:puzzlePhotoTag];
        puzzleBG = (UIImageView *)[view viewWithTag:puzzleBGTag];
        label = (UILabel *)[view viewWithTag:labelTag];
    }

    //*************************************************
    //do setup that is different depending on index here
    //*************************************************

    //! get friend's photo
    FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
    UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];

    if (imgPuzzle)
        puzzlePhoto.image = imgPuzzle;
    else
        puzzlePhoto.image = [UIImage imageNamed:@"puzzle background_small.png"];

    if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) || ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
        puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg_me.png"];
    else
        puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg.png"];

    //! get friend name
    FUser *userFriend = foresightCurrent.m_userFriend;
    label.text = userFriend.m_strName;

    return view;
}

スクロールの慣性の問題については、この修正を適用した後もまだ表示される場合は、github ページで別のバグ レポートを提出してください。

于 2012-11-22T09:30:57.690 に答える