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

问题描述 投票:0回答:3

我正在尝试使用

Leading
启用
Trailing
drag
长按桌面视图单元格至
drop
Swift
选项。在这里,我使用下面的代码,我可以拖放它,但无法长按,也无法一次启用前导和尾随
swipe
。应用程序启动时需要默认启用三件事。

表格视图委托

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()
    }
ios swift uitableview
3个回答
1
投票

您可以通过以下方式做到这一点...

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
}

// 左行动领先

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

0
投票

这是对我有用的东西,但我不确定它是否正确并且将来永远有效

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 []
    }
}

0
投票

你修好了吗?现在我也有同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.