使用长按手势对表格视图中的单元格重新排序?

2024-04-14

我希望能够使用长按手势(而不是使用标准重新排序控件)对表格视图单元格重新排序。识别长按后,我希望 tableView 实质上进入“编辑模式”,然后重新排序,就像我使用 Apple 提供的重新排序控件一样。

有没有一种方法可以在不需要依赖第三方解决方案的情况下做到这一点?

提前致谢。

编辑:我最终使用了已接受答案中的解决方案并依赖于第三方解决方案。


他们在 iOS 11 中添加了一种方法。

首先,启用拖动交互并设置拖放委托。

然后实现 moveRowAt,就像使用重新排序控件正常移动单元格一样。

然后实现拖/放委托,如下所示。

tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
tableView.dropDelegate = self

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { }

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

extension TableView: UITableViewDropDelegate {
    func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {

        if session.localDragSession != nil { // Drag originated from the same app.
            return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
        }

        return UITableViewDropProposal(operation: .cancel, intent: .unspecified)
    }

    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用长按手势对表格视图中的单元格重新排序? 的相关文章

随机推荐