Ios DiffableDataSource-是否有办法限制只能在同一节中执行重新排序?
在中,苹果展示了一个关于如何使用Ios DiffableDataSource-是否有办法限制只能在同一节中执行重新排序?,ios,swift,uikit,Ios,Swift,Uikit,在中,苹果展示了一个关于如何使用DiffableDataSource ReorderableListViewController.swift 是否有办法限制只能在同一区段内执行重新排序? 在reorderingHandlers.canReorderItem中我们做不了什么,因为闭包参数item指的是我们正在拖动的当前源项。没有关于目标项的信息,我们可以将其与进行比较以决定是否返回true或false。对于您的数据源来说,这种行为不是问题。这是你的代表的问题 你可以用 确定是否允许移动 func
DiffableDataSource
ReorderableListViewController.swift
是否有办法限制只能在同一区段内执行重新排序?
在
reorderingHandlers.canReorderItem
中我们做不了什么,因为闭包参数item
指的是我们正在拖动的当前源项。没有关于目标项的信息,我们可以将其与进行比较以决定是否返回true或false。对于您的数据源来说,这种行为不是问题。这是你的代表的问题
你可以用
确定是否允许移动
func collectionView(_ collectionView: UICollectionView,
targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath,
toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
let sourceSection = sourceIndexPath.section
let destSection = proposedDestinationIndexPath.section
var destination = proposedDestinationIndexPath
if destSection < sourceSection {
destination = IndexPath(item: 0, section: sourceSection)
} else if destSection > sourceSection {
destination = IndexPath(item: self.backingStore[sourceSection].count-1, section: sourceSection)
}
return destination
}
func collectionView(collectionView:UICollectionView,
targetIndexPathForMoveFromItemAt originalIndexPath:IndexPath,
toProposedIndexPath proposedIndexPath:IndexPath)->IndexPath{
让sourceSection=sourceIndexPath.section
设destSection=proposedDestinationIndexPath.section
var destination=proposedDestinationIndexPath
如果destSectionsourceSection,则为else{
destination=IndexPath(项:self.backingStore[sourceSection].count-1,section:sourceSection)
}
返回目的地
}
这会将项目的移动限制到其自己的部分。您也可以在委托方法collectionView(\uU9:targetIndexPathForMoveFromItemAt:TopProposedIndexPath:)中使用此类记录 我在Matt Neuburg的《编程iOS 14》一书中读到了这一点,效果很好:)
func collectionView(_ collectionView: UICollectionView,
targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath,
toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
let sourceSection = sourceIndexPath.section
let destSection = proposedDestinationIndexPath.section
var destination = proposedDestinationIndexPath
if destSection < sourceSection {
destination = IndexPath(item: 0, section: sourceSection)
} else if destSection > sourceSection {
destination = IndexPath(item: self.backingStore[sourceSection].count-1, section: sourceSection)
}
return destination
}
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
if originalIndexPath.section != proposedIndexPath.section {
return originalIndexPath
}
return proposedIndexPath
}