如何删除 iCarousel 中的重复项

Posted

技术标签:

【中文标题】如何删除 iCarousel 中的重复项【英文标题】:How to remove duplicated items in iCarousel 【发布时间】:2012-11-21 13:27:07 【问题描述】:

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;

【问题讨论】:

我使用了 iCarousel 1.6.3 beta。所以我刚刚升级了 1.7.2(最新版本)。虽然背景图像被删除,但它不能灵活滚动。换句话说,它没有惯性。 如果您可以将您放入的代码粘贴到您的 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 方法中,将会很有帮助。 虽然我将您的示例代码(在 iCarousel-master.zip,iCarousel 1.7.2 中)放入我的项目中,但它也不能灵活滚动 【参考方案1】:

这个问题几乎可以肯定是由于视图被错误地回收。视图与不同的索引一起使用,您永远不应该在视图创建代码中设置特定于索引的属性。以下是设置项目视图的正确方法:

- (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 页面上提交单独的错误报告。

【讨论】:

谢谢你的回答,虽然我跟着你,但还是一样。 如果轮播在释放时没有继续滚动,可能与1.7.2中定时器的变化有关。尝试使用 github 上标签页面中的 1.7.1,看看是否有所不同。 嘿@NickLockwood,我已经尝试过这种方式,但是遇到了不匹配的问题,我正在使用 iCarouselTypeRotary 和 iCarouselOptionWrap == true 。【参考方案2】:

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

[carousel performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

【讨论】:

以上是关于如何删除 iCarousel 中的重复项的主要内容,如果未能解决你的问题,请参考以下文章

Oracle SQL:如何删除 listagg 中的重复项

如何删除 MySQL 表中的重复项

如何删除 MongoDb 中的重复项?

如何从 Perl 中的数组中删除重复项?

如何使用两个键删除数组中的重复项?

如何删除数组中的重复项