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

标签 ios swift uitableview drag-and-drop ios11

我知道在使用新的 UITableViewDropDelegateUITableViewDragDelegate 委托(delegate)时可以一次对单个项目/单元格重新排序,但是否可以支持处理多个。

例如,在这张截图中,我拿着一个元素: Dragging a single item/cell

然后放下单元格将其放置到位。

然而,当我抓取多个单元格时,我得到禁止进入的标志,并且单元格不会重新排序: Multiple selection Drag and Drop with no entry sign

如果我从另一个应用程序中获取多个项目,它工作正常,例如从 iMessage 中拖出多条消息: Multiple selection Drag and Drop from iMessage

在重新排序仅包含本地项目的 TableView 时是否可以这样做,以便您可以更快地重新排序?

这是我的代码:

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
    }
}

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
    }
}

viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

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

    tableView.dragInteractionEnabled = true
}

最佳答案

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

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

Table view moveRowAt IndexPath to IndexPath. You can continue to implement this, if you like, to support reordering, using Drag and Drop. Because table view is actually going to call this instead of calling through perform drop with coordinator, if you've returned that magic drop proposal and a single row is actually being reordered.

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

关于ios - 是否可以使用 iOS 11 拖放在 UITableView 中一次重新排序多个项目/单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45169912/

相关文章:

ios - tableview 内的 UITextView 不根据内容调整大小

swift - 使用 Alamofire 处理超时

ios - NSFetchedResultsController 只获取前 10 个项目

ios - 滚动时未调用 sizeForItemAt

ios - MobileFirst 8 迁移 : How to generate same device UUID in iOS

ios - 如何根据 UITableView 中的文本增加 UILabel 高度?

ios - 使用 PFFile 查找数组中元素的位置。 swift

objective-c - NSMutableArray 只初始化一次

ios - 初始初始化后添加/编辑 pod 文件 "firebase"

ios - 如何更改 Rev Mob Banner Ad 在 iOS 应用中的位置?