簡體   English   中英

Swift 創建左、右滑動,長按 tableview 行單元格可移動

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

在我的場景中,我正在嘗試創建一個 UITableView 單元格,通過長按 tableView 單元格啟用左右滑動以在特定部分內移動。 在這里,我只能進行尾隨和前導滑動,不知道如何在一個部分內移動單元格。

下面的前導和尾隨代碼

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) 在表格視圖上啟用拖動交互 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