2

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

我将使用 iCarousel 线性显示图像。这些图像将被动态获取。假设有 2 个图像。但是当我更新 iCarousel 时,背景中也有 2 个图像,如下图。


在调试代码时,我发现 viewForItemAtIndex 函数在我调用reloadData函数后被调用了 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 回答
0

我通过在 mainThread 上执行 reloadData 解决了这个问题

[carousel performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
于 2013-05-02T13:52:50.937 回答