简体   繁体   中英

How to stop UICollectionViewDiffableDataSource to scroll the collection view while we apply snapshot?

Is there any way that I can make UICollectionViewDiffableDataSource stop scrolling collection view while we apply snapshot?

I am using this library: https://github.com/ra1028/DiffableDataSources

I have a situation where I want to keep the collection view scroll position while I load more data using UIRefreshControl.

var snapshot = DiffableDataSourceSnapshot<ChatKitDataSourceSection, ChatKitMessage>()
snapshot.deleteAllItems()
snapshot.appendSections(self.items)
self.items.forEach { section in
    snapshot.appendItems(section.rowItems, toSection: section)
}
self.apply(snapshot, animatingDifferences: true, completion: completion)

That's how I am updating the snapshot every time I load more data.

Can you ensure your for your model that conforms to Hashable, that you're not setting a new UUID() for your id every time you apply your snapshot. This will cause a reload that will scroll back to the top.

Ensure you have an id for your model that is unique per instance but wont get overwritten if you have the same data when applying a new snaphot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM