4

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.

Stephen
  • 1,427
  • 1
  • 17
  • 42
  • Are you updating snapshot after `deleteItem` ? – pdubal Jan 29 '20 at 17:08
  • Yes deleting from the existing snapshot or creating a new snapshot result in the same behavior – Stephen Jan 29 '20 at 20:22
  • 1
    Have you resolved this issue? I am having a similar issue: the deleted cell gets replaced by something at the bottom of current visible cells. I observed two strange things: first, all my visible cells get re-loaded somehow, and second, the wrong layout was ultimately fixed by a layoutSubviews called from some unknown origin. In any case, the fact that every cell gets re-loaded alone makes feel quite nervous. – Minsheng Liu Mar 08 '20 at 12:29
  • I think this [answer](https://stackoverflow.com/a/57997005/1526408) gets it. – Minsheng Liu Mar 08 '20 at 13:56
  • 1
    I did not resolve it. I did try the delay but it didn't work well for me. Some devices I tested on required more than the 0.4 seconds suggested there. Such a long delay was not really acceptable for my use case – Stephen Mar 09 '20 at 13:17

0 Answers0