UITableView尾部轻扫vs长或长击后返回单元

问题描述:

在我的表格视图中,我实现了func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?以提供删除功能。我想确认删除,因此我显示带删除或取消提示的UIAlertController。当用户决定取消时,单元格应该恢复为不编辑。通常这通过调用tableView?.setEditing(false, animated: true)工作,但是这不适用于长时间刷卡。UITableView尾部轻扫vs长或长击后返回单元

理想情况下,我希望tableView?.setEditing(false, animated: true)即使在单元格被完全滑过时也能正常工作。

其他选项是简单地省略完整刷卡的确认,但后来我需要一种方法来确定是长按还是短按。

如何实现这些结果的方式是什么?

感谢

+0

原来我可能是错的,现在setEditing(false animated:true)即使是短暂的滑动也不会执行任何操作。因此这个问题可能在别处。 –

我正在处理同样的问题现在。我尝试了一切,但分辨率很简单。当您创建UIContextualAction时,它将处理程序作为一个参数。处理程序有点像关闭。闭包有3个参数。最后一个是完成处理程序。只要你完成,只需调用这个处理程序为true,你就完成了。我希望它有帮助。

let removeHandler:UIContextualActionHandler = { (action, view, finished) in 
     let alert = UIAlertController(title: "Warning", message: "Do you really want to remove this?", preferredStyle: .alert) 

     let removeButton = UIAlertAction(title: "Remove", style: .destructive, handler: { (action) in 
      finished(true) 
      CameraManager.shared.removeCamera(index: indexPath.row) 
      tableView.deleteRows(at: [indexPath], with: .automatic) 
     }) 

     let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 

     alert.addAction(removeButton) 
     alert.addAction(cancelButton) 
     self.present(alert, animated: true) 
    } 
    let removeAction = UIContextualAction(style: .destructive, title: "Odstrániť", handler: removeHandler) 

    let conf = UISwipeActionsConfiguration(actions: [removeAction])