Swift - 如果检测到左侧 "Swipe to delete",如何隐藏自定义 tableViewCell class 中的按钮?

Swift - How to hide button in custom tableViewCell class if left "Swipe to delete" detected?

我有一个自定义单元格 class,其中有 2 个标签显示一个词及其翻译。当用户点击其中一个标签时,我想打开一个只显示标签内容的新视图控制器。我们需要了解点击了哪个标签。

我无法使用 tapGestureRecognizer 和标签上的标签使其工作,因为标签由 class 文件管理。我发现在标签顶部添加透明按钮更容易。

新视图控制器的转接工作正常。但是我不能再在 tableViewCells.

上使用 "swipe to delete"

当我们向左滑动时,该行向左移动,它在该行的右侧显示红色方块 "DELETE" 按钮,但同时它对新的 viewController.

How can I hide / deactivate buttons in tableViewCell if left "Swipe to delete" is detected?

我尝试添加一个左swipeGestureRecognizer和添加一个公共变量isSwipingLeft: Bool到不同的地方(viewDidEndEditing ...等)没有成功。我做的最好的是检测滑动,隐藏按钮,但无法取消隐藏按钮......

编辑:

DetailListCell

protocol CustomCellDelegate {
func leftButtonTapped(cell: DetailListCell)
func rightButtonTapped(cell: DetailListCell)
}

class DetailListCell: UITableViewCell {

@IBOutlet weak var entryLeftLbl: UILabel!
@IBOutlet weak var entryRightLbl: UILabel!
@IBOutlet weak var leftButtonOutlet: UIButton!
@IBOutlet weak var rightButtonOutlet: UIButton!
var delegate: CustomCellDelegate?

func configureDetailListCell(entry: Entry) {
entryLeftLbl.isUserInteractionEnabled = true
entryRightLbl.isUserInteractionEnabled = true
// cell configuration to hide/display labels and buttons ...
}

@IBAction func leftButton(_ sender: Any) {
if isSwipingToDelete == false {
delegate?.leftButtonTapped(cell: self)
}}   

@IBAction func rightButton(_ sender: Any) {
if isSwipingToDelete == false {
delegate?.rightButtonTapped(cell: self)
}}}

DetailListVC

override func viewDidLoad() {
detailTableView.delegate = self
detailTableView.dataSource = self        

// Swipe left
let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(DetailListVC.swipeLeft(gestureRecognizer:)))
swipeLeft.direction = .left
self.view!.addGestureRecognizer(swipeLeft)
}

// Swipe left to detect Swipe to delete
func swipeLeft(gestureRecognizer: UISwipeGestureRecognizer) {
isSwipingToDelete = true
}

func leftButtonTapped(cell: DetailListCell) {
// Find the row of the textField
let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!        
// Find the Entry object of the row/textField selected
if let objs = controller.fetchedObjects , objs.count > 0 {
let item = objs[indexPath.row].faceA
performSegue(withIdentifier: "FullEntryVC", sender: item)
}}

func rightButtonTapped(cell: DetailListCell) {        
// Find the row of the textField
let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!        
// Find the Entry object of the row/textField selected
if let objs = controller.fetchedObjects , objs.count > 0 {
let item = objs[indexPath.row].faceB
performSegue(withIdentifier: "FullEntryVC", sender: item)
}}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FullEntryVC" {
if let destination = segue.destination as? FullEntryVC {
if let entryFace = sender as? String {
isEditMode = false
destination.entryLabelValue = entryFace
}}}}

正如 Valdmer 所建议的,我使用了 isEditing 属性 of UITableViewCell。我在我的按钮方法中添加了一个 if 语句来检查 if cell.isEditing == false。如果 falseperformSegue(withIdentifier: ) 为 运行,如果 true 则 "swipe to delete" 工作正常且按钮不执行任何操作。

我删除了与 NSNotificationswipeLeft(gestureRecognizer: ) 相关的所有代码。

这是其中一个按钮的更新代码:

func leftButtonTapped(cell: DetailListCell) {
    // Check if "swipe to delete" is detected:
    if cell.isEditing == false {

        // Find the row of the textField
        let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!

        // Find the Entry object of the row/textField selected
        if let objs = controller.fetchedObjects , objs.count > 0 {
            let item = objs[indexPath.row].faceA
            performSegue(withIdentifier: "FullEntryVC", sender: item)
        }
    }
}