I'm trying to use NSDiffableDataSource with my custom CollectionView layout. The position and size of each cell depends on the number of elements in the collection. It also uses UIDynamicAnimator to produces springy behaviour. The problem is when a new cell added or one removed, the layout for others is not changing, even if I call .invalidateLayout() after applying a new snapshot. Where should I call it? Or maybe, should I use any other approach?
func applySnapshot(animatingDifferences: Bool = true) {
var snapshot = Snapshot()
snapshot.appendSections(["main"])
snapshot.appendItems(models)
self?.collectionView.collectionViewLayout.invalidateLayout()
// Does not change anything
dataSource.apply(snapshot, animatingDifferences: animatingDifferences, completion: { [weak self] in
self?.collectionView.collectionViewLayout.invalidateLayout()
// Does not change anything
})
}
When I was using the old UIDataSource approach, everything worked fine. I was invalidating Layout before reloading my collection
public func update(profiles: [CreatorProfileProtocol]) {
let models = profiles. map {...}
self.models = models
(collectionView.collectionViewLayout as? CustomSpringyLayout)?.updateContentSize(for: models.count)
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()
}
I wanted more smooth update of my collection, so I moved to DiffableDataSource approach. But I can't find any usage of custom layouts that use invalidation and recomputing.