首页 > 解决方案 > 滑动以从 TableView 中删除不起作用

问题描述

我只是想将“滑动删除功能”添加到我的tableView,据我所知,我唯一需要添加的是这两个功能:

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

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        self.deleteWishDelegate?.deleteWish(indexPath)
    }
}

但这无济于事。我不能刷卡。我注意到的一件奇怪的事情是,我的整体顶部View有一个TransitionView ,我不知道它是从哪里来的。但是我可以点击Button里面的 atableViewCell所以我认为它不会阻止任何东西。

有人知道这里发生了什么吗?如果您需要更多详细信息,请告诉我。

更新:

正如评论中所建议的,我使用了另一个函数来完成工作:

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .destructive, title: "Löschen") { _, _, completionHandler in
        self.deleteWishDelegate?.deleteWish(indexPath)
    completionHandler(true)
    }
    deleteAction.backgroundColor = .red
    deleteAction.image = UIImage(systemName: "trash")
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
    configuration.performsFirstActionWithFullSwipe = false
    return configuration
}

还有我的 delteWsihDelegate 函数:

extension WishlistViewController: DeleteWishDelegate {
    func deleteWish(_ idx: IndexPath){
        // remove the wish from the user's currently selected wishlist
        wishList.wishes.remove(at: idx.row)
        // set the updated data as the data for the table view
        theTableView.wishData.remove(at: idx.row)
        self.theTableView.tableView.deleteRows(at: [idx], with: .right)
        print("deleted")
    }
}

现在的问题是,我每刷 20 到 30 次才有效。任何人都知道为什么会发生这种情况?

更新 2

我发现了问题所在。我也有GestureRecognizer同样的看法。删除该功能后,该功能可以正常工作。有没有办法让两者同时工作?

标签: iosswiftuitableview

解决方案


实现UITableViewDelegate方法

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath)

可用于配置滑动操作 - 在您的情况下为删除。这是我在一个项目中使用的示例:

extension ExampleViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive,
                                              title: "Delete") { [weak self] _, _, complete in
            tableView.deleteRows(at: [indexPath], with: .automatic)
            complete(true)
        }
        deleteAction.backgroundColor = .red

        return UISwipeActionsConfiguration(actions: [deleteAction])
    }

    // instead of a gesture recognizer you can use this delegate method
    // it will be called whenever you tap on a cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // whatever you gesture recognizer did
    }
}

由于您在整个视图上的 GestureRecognizer 会给您带来麻烦,因此我建议您使用第三方实现或苹果公共 API 来处理关闭您的 viewController 而不是自己实现它。在这种情况下,您可以使用 viewController 的 UIModalPresentationStyle。

func startExampleViewController() {
   exampleViewController.modalPresentationStyle = .formSheet
   previousViewController.present(exampleViewController, animated: true)
}

推荐阅读