我知道在使用新的 UITableViewDropDelegate
和 UITableViewDragDelegate
委托(delegate)时可以一次对单个项目/单元格重新排序,但是否可以支持处理多个。
然后放下单元格将其放置到位。
然而,当我抓取多个单元格时,我得到禁止进入的标志,并且单元格不会重新排序:
如果我从另一个应用程序中获取多个项目,它工作正常,例如从 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/