【问题标题】:How can I delete an item from a collection view without animation?如何在没有动画的情况下从集合视图中删除项目?
【发布时间】:2016-10-06 23:51:08
【问题描述】:

我有一个支持集合视图的项目 ID 数组。当用户滚动浏览集合视图时,每个单元格获取该 ID 的对象,然后获取与该对象关联的图像。如果一个对象碰巧没有与之关联的图像,我想从数组中删除该项目 ID,然后更新屏幕上的集合视图。问题是有很多没有图像的对象(大量更新),我需要立即更新集合视图(没有时间动画)。

使用reloadData 会导致集合视图在每个对象被删除时闪烁。使用deleteItemsAtIndexPaths 需要不受欢迎的动画。有什么方法可以从没有动画的集合视图中删除不是reloadData 的项目?

【问题讨论】:

    标签: ios uicollectionview uicollectionviewlayout


    【解决方案1】:

    这是一个 Swift 5 版本,其实现方式略有不同:

    UIView.performWithoutAnimation {
        self.collectionView.performBatchUpdates({
            self.collectionView.insertItems(at: indexPaths)
        }, completion: nil)
    }
    

    【讨论】:

      【解决方案2】:

      您需要使用禁用动画选项重新加载特定行。

      [UIView setAnimationsEnabled:NO];
      
      [collectionView performBatchUpdates:^{
          [collectionView reloadItemsAtIndexPaths:indexPaths];
      } completion:^(BOOL finished) {
          [UIView setAnimationsEnabled:YES];
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多