简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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