In https://developer.apple.com/documentation/uikit/views_and_controls/collection_views/implementing_modern_collection_views , Apple has shown a simple example on how to perform reordering using DiffableDataSource
ReorderableListViewController.swift
dataSource.reorderingHandlers.canReorderItem = { item in return true }
dataSource.reorderingHandlers.didReorder = { [weak self] transaction in
guard let self = self else { return }
// method 1: enumerate through the section transactions and update
// each section's backing store via the Swift stdlib CollectionDifference API
if self.reorderingMethod == .collectionDifference {
for sectionTransaction in transaction.sectionTransactions {
let sectionIdentifier = sectionTransaction.sectionIdentifier
if let previousSectionItems = self.backingStore[sectionIdentifier],
let updatedSectionItems = previousSectionItems.applying(sectionTransaction.difference) {
self.backingStore[sectionIdentifier] = updatedSectionItems
}
}
// method 2: use the section transaction's finalSnapshot items as the new updated ordering
} else if self.reorderingMethod == .finalSnapshot {
for sectionTransaction in transaction.sectionTransactions {
let sectionIdentifier = sectionTransaction.sectionIdentifier
self.backingStore[sectionIdentifier] = sectionTransaction.finalSnapshot.items
}
}
}
Is there any way, to limit the reordering can only be performed within the same section?
There is not much we can do in reorderingHandlers.canReorderItem, because the closure parameter item is referring to the current source item we are dragging. There is no information on destination item, which we can compare with to decide whether to return true or false.