首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用拖放UITableview单元快速实现前导和尾随滑动

如何使用拖放UITableview单元快速实现前导和尾随滑动
EN

Stack Overflow用户
提问于 2019-12-30 05:57:26
回答 2查看 1.9K关注 0票数 0

我正在尝试使用长按表单元格使LeadingTrailing滑动到使用Swiftdragdrop选项。在这里,我使用下面的代码,我可以拖放它,但是不能做长按压,也不能一次启用引导和尾随swipe。在应用程序启动时,默认情况下需要启用三项功能。

Tableview代理

代码语言:javascript
复制
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()
    }
EN

回答 2

Stack Overflow用户

发布于 2019-12-30 06:07:01

你可以这样做.

代码语言:javascript
复制
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领导

代码语言:javascript
复制
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])
}
票数 1
EN

Stack Overflow用户

发布于 2021-03-18 23:19:24

这里有一些对我有用的东西,但我不确定它是否正确,而且将来还会一直有效

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59526339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档