简体   繁体   English

快速禁用表格视图单元格中的滑动操作

[英]Disable the swipe action in table view cell in swift

I have implemented the swipe action for the table view cell and they are two actions: Accept and Reject . 我已经为表格视图单元实现了滑动操作,这是两个操作: AcceptReject

When I swipe for the first time it must appear and for the second time if I swipe the cell it should not open. 第一次滑动时,它必须出现;第二次滑动单元格时,它不应该打开。

The swipe action for the cell must be in the disabled mode. 单元格的滑动操作必须处于禁用模式。

First, you should keep track of the indexPath of cells you have interacted with. 首先,您应该跟踪与之交互的单元的indexPath。

private var disabledCellsIndexPath = [IndexPath]()

Then in the method to configure your swipe actions ( this is the new iOS 11 API ): 然后在配置滑动操作的方法中( 这是新的iOS 11 API ):

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let action = UIContextualAction(style: .normal, title: "Action Title", handler: { _, _, completionHandler in

        // DO WHAT YOU WANT HERE
        // …

        // This is where you specify that this specific cell should be 'disabled'
        self?.disabledCellsIndexPath.append(indexPath)

        completionHandler(true)
    })

    return UISwipeActionsConfiguration(actions: [action])
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return !disabledCellsIndexPath.contains(indexPath)
}

If I understood your question correctly, Please follow following generalize idea, 如果我正确理解了您的问题,请遵循以下概括性思路,

You can manage the flag ?for that indexPath? 您可以管理该indexPath的标志? in app end database. 在应用结束数据库中。 Whenever required open or disable ie manage it you can enable/disable the edit action for that indexPath. 每当需要打开或禁用(即对其进行管理)时,都可以启用/禁用该indexPath的编辑操作。

Use allowEdit flag when you want swipe Enable . swipe Enable时,请使用allowEdit标志。 else make it false. 否则将其设为虚假。

var allowEdit: Bool = true
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return allowEdit
    }



     func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
        {
           if allowEdit{
               /*to perform Accept and Reject action on row*/
           }
    }

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

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