How can I use UICollectionViewDiffableDataSource reorderHandlers with a custom compositional layout?
Asked Answered
S

1

5

As of iOS 14, UICollectionViewDiffableDataSource has a reorderHandlers property. It's demonstrated in some sample code and talked about in WWDC 2020 session on Advances in Diffable Data Sources. The presenter states that you have to provide a canReorder and didReorder closure to enable the feature.

The sample code uses it in a collection view with a list layout configuration, and configures the list cells with reorder accessories. The canReorder and didReorder methods are called as expected. But if I remove the reorder accessories from the cells, reordering no longer works - it doesn't call either closure. It also doesn't work in my app, where I have a grid layout using a compositional layout.

How do I enable reordering on UICollectionViewDiffableDataSource without list cells and reorder accessories?

Sherrer answered 4/11, 2021 at 18:32 Comment(1)
I talked to a UIKit engineer about this in a Tech Talk and it sounds like this is a bug. I filed Feedback FB9753149 in case anyone wants to dupe (or a UIKit engineer finds this post).Sherrer
S
2

I talked to another engineer in a virtual lab in WWDC 2022 about this, and it seems that the reorder accessory is just a convenience. The functionality can be achieved more manually too. Documentation is here under "Reorder Items Interactively". We can use our own gesture recognizer to know when to start dragging, and call beginInteractiveMovement after figuring out which item to move.

canReorderItem must return true for this to work.

YMMV. Have fun!

Sherrer answered 7/9, 2022 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.