Animation Glitch When Deleting UICollectionViewDiffableDataSource Item From Context Menu

1.2k Views Asked by At

I have adopted the new UICollectionViewDiffableDataSource. I am applying a datasource snapshot everytime I delete an item:

var snapshot = NSDiffableDataSourceSnapshot<Int, Item>()
snapshot.appendSections([0])
snapshot.appendItems(items)
apply(snapshot, animatingDifferences: true)

The delete is offered through the built in collection view configuration option:

func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    guard let item = dataSource.itemIdentifier(for: indexPath) else {
        return nil
    }

    let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
        let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: .destructive) { _ in
            self.deleteItem(item)
        }

        return UIMenu(title: "", image: nil, identifier: nil, children: [delete])
    }
    return configuration
}

If I delete the item from outside the context menu, the animation works great. If I delete from the context menu then one cell disappears and then causes the next to flash. I suspect there is some sort of conflict between closing the context menu & running the delete animation. I'm looking for a way to work around this.

0

There are 0 best solutions below