是否可以使用iOS 11拖放功能在UITableView中一次重新排序多个项目/单元格?

Rad*_*her 6 drag-and-drop uitableview ios swift ios11

我知道在使用new UITableViewDropDelegateUITableViewDragDelegatedelegates 时可以一次重新排序单个项目/单元格,但是是否可以支持处理多个项目/单元格.

例如,在此屏幕截图中,我持有一个项目: 拖动单个项目/单元格

放下牢房就把它放进去了.

然而,当我抓住多个单元格时,我得到了无条目符号,并且单元格不会重新排序: 多项选择拖放,没有输入符号

如果我来自另一个应用程序的多个项目,它可以正常工作,例如从iMessage中拖出多个消息: 多次选择从iMessage拖放

是否可以在仅使用本地项目重新排序表格视图时执行此操作,以便您可以更快地重新排序?

这是我的代码:

UITableViewDragDelegate

extension DotViewController: UITableViewDragDelegate {
    func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        return [getDragItem(forIndexPath: indexPath)]
    }

    func tableView(_ tableView: UITableView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
        return [getDragItem(forIndexPath: indexPath)]
    }

    func getDragItem(forIndexPath indexPath: IndexPath) -> UIDragItem {
        // gets the item
    }
}
Run Code Online (Sandbox Code Playgroud)

UITableViewDropDelegate

extension DotViewController: UITableViewDropDelegate {
    func tableView(_ tableView: UITableView, canHandle session: UIDropSession) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
        return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
    }

    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
        // Handles Drop
    }
}
Run Code Online (Sandbox Code Playgroud)

viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    tableView.dragDelegate = self
    tableView.dropDelegate = self

    tableView.dragInteractionEnabled = true
}
Run Code Online (Sandbox Code Playgroud)

And*_*ant 1

您可以通过拖动委托(例如,section.row 的数组)提供对所有选定单元格的引用,然后实现 tableView:performDropWith:withCoordinator 对它们进行重新排序,从而自行进行多行重新排序。(我怀疑你知道这一点)

如果您想通过返回 UITableViewDropProposal(operation: .move, Intent: .insertAtDestinationIndexPath) 的删除提案来支持重新排序,那么 UIKit 使用 iOS 11 之前的 tableView:moveRowAt:to 函数,那么这只支持单行。

表视图 moveRowAt IndexPath 到 IndexPath。如果您愿意,您可以继续实现此功能,以支持使用拖放重新排序。因为表视图实际上会调用它,而不是通过协调器执行 drop 来调用,如果您返回了那个神奇的 drop 提案并且实际上正在重新排序单行。

来源:https ://developer.apple.com/videos/play/wwdc2017/223/?time=1836