简体   繁体   English

Swift 创建左、右滑动,长按 tableview 行单元格可移动

[英]Swift create left, right swipe with long press tableview row cell movable

In my scenario, I am trying to create a UITableView cell enable left and right swipe with tableView cell long-press to movable within in particular section.在我的场景中,我正在尝试创建一个 UITableView 单元格,通过长按 tableView 单元格启用左右滑动以在特定部分内移动。 Here, I can only able to do trailing and leading swipe, don't know how to move cells within a section.在这里,我只能进行尾随和前导滑动,不知道如何在一个部分内移动单元格。

Below Code For Leading and Trailing下面的前导和尾随代码

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
            print("OK, marked as Delete")
            success(true)
        })
        if #available(iOS 13.0, *) {
            deleteAction.image = UIImage(systemName: "delete")
        } else {
            // Fallback on earlier versions
            deleteAction.image = UIImage(named: "delete")
        }
        deleteAction.backgroundColor = .red
        return UISwipeActionsConfiguration(actions: [deleteAction])
}

func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let modifyAction = UIContextualAction(style: .normal, title:  "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
            print("Update action ...")
            self.showaddMilestone()
            success(true)
        })
        modifyAction.image = UIImage(named: "edit")
        modifyAction.backgroundColor = .green
        return UISwipeActionsConfiguration(actions: [modifyAction])
}

a) enable drag interation on table view b) set drag and drop delegates a) 在表格视图上启用拖动交互 b) 设置拖放委托

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

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

extension TableView: UITableViewDropDelegate,UITableViewDragDelegate {

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

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

     if session.localDragSession != nil { 
        return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
    }

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

func tableView(_ tableView: UITableView, performDropWith coordinator: 
 UITableViewDropCoordinator) {
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM