简体   繁体   English

如何通过拖放 UITableview Cell Swift 启用前导和尾随滑动

[英]How to enable Leading and Trailing Swipe with Drag and Drop UITableview Cell Swift

I am trying to enable Leading and Trailing swipe with Long press tableview cell to drag and drop option using Swift .我试图让LeadingTrailing用长按的tableview细胞轻扫dragdrop使用选项Swift Here, I am using below code I can able to drag and drop it but can't able to do long press also can't able to enable leading and trailing swipe at a time.在这里,我使用下面的代码我可以拖放它但不能长按也不能一次启用前导和尾随swipe Three things need to enable by default when app launched.应用程序启动时需要默认启用三件事。

Tableview Delegate表视图委托

override 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)
        })
        deleteAction.backgroundColor = .orange
        return UISwipeActionsConfiguration(actions: [deleteAction])
    }

    override 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 = .red
        return UISwipeActionsConfiguration(actions: [modifyAction])
    }

 override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let movedObject = self.milestoneTitles[sourceIndexPath.row]
        milestoneTitles.remove(at: sourceIndexPath.row)
        milestoneTitles.insert(movedObject, at: destinationIndexPath.row)
        debugPrint("\(sourceIndexPath.row) => \(destinationIndexPath.row)")
        // To check for correctness enable: self.tableView.reloadData()
    }

You can do this in following way...您可以通过以下方式执行此操作...

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    //EDIT
    let actionEDIT =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
        //do stuff
        completionHandler(true)
    })
    actionEDIT.image = UIImage(named: "icn_edit")
    actionEDIT.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")

    //PDF
    let actionPDF =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
        //do stuff
        completionHandler(true)
    })
    actionPDF.image = UIImage(named: "icn_pdf")
    actionPDF.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")

    //SHARE
    let actionSHARE =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
        //do stuff
        completionHandler(true)
    })
    actionSHARE.image = UIImage(named: "icn_shareGreen")
    actionSHARE.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")

    let configuration = UISwipeActionsConfiguration(actions: [actionSHARE,actionPDF,actionEDIT])

    return configuration
}

// leftAction Leading // leftAction 领先

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let leftAction = UIContextualAction(style: .normal, title:  "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("leftAction tapped")
        success(true)
    })

    leftAction.image = UIImage(named: "")
    leftAction.backgroundColor = UIColor.red

    return UISwipeActionsConfiguration(actions: [leftAction])
}

Here's something that works for me but I'm not sure if it's correct and will always work in future这是对我有用的东西,但我不确定它是否正确并且将来总是有效

final class ViewController: UITableViewController {

    private let data: [String] = [
        "1", "2", "3", "4", "5"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dragInteractionEnabled = true
        tableView.dragDelegate = self
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        cell.detailTextLabel?.text = data[indexPath.row]
        return cell
    }

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

    override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion ) in
            completion(true)
        }
        return UISwipeActionsConfiguration(actions: [delete])
    }
}

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

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

相关问题 Swift UITableView自定义单元格排序拖放? - Swift UITableView custom Cell ordering Drag Drop? 在 UITableView Swift 4 的主要滑动操作中不显示标题 - Not displaying the title in leading swipe actions for UITableView Swift 4 如何确定UITableView前后滑动动作(iOS 11)的当前活动位置? - How do i identify the current active position of UITableView Leading & Trailing Swipe Actions (ios 11)? UITableView 将一个单元格拖放到另一个单元格上? - UITableView drag and drop a cell onto another cell? 滑动以删除UITableView单元格动画在swift 2中不起作用 - swipe to delete UITableView cell animation is not working in swift 2 UITableView:通过拖放将单元格移动到另一个位置 - UITableView: move cell to a position to another with drag & drop UITableView尾随即扫随即随即长扫或长扫后返回单元格 - UITableView trailing swipe short vs long or return cell after long swipe 如何在像Trello app这样的单元格中具有UITableView内部的UICollectionView中进行拖放 - How to do Drag and Drop in UICollectionView that has UITableView inner in cell like Trello app 如何启用滑动以删除TableView中的单元格? - How to enable swipe to delete cell in a TableView? 如何拖动UITableViewCell并将其拖放到另一个UITableView? - How to drag UITableViewCell and drop it to another UITableView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM