简体   繁体   English

滑动以从 TableView 中删除不起作用

[英]Swipe to delete from TableView not working

I simply want to add the "swipe-to-delete-function" to my tableView and as far as I know the only thing I have to add are these two functions:我只是想将“滑动删除功能”添加到我的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)
    }
}

But this doesn't do anything.但这无济于事。 I can not swipe.我不能刷卡。 A weird thing that I noticed is that my whole View has a TransitionView on top of it, which I have no idea where it comes from.我注意到的一件奇怪的事情是,我的整个View上面都有一个TransitionView ,我不知道它是从哪里来的。 However I can click a Button inside a tableViewCell so I don't think its blocking anything.但是,我可以单击tableViewCell内的Button ,所以我认为它不会阻塞任何东西。

Any one any idea what is going on here?有人知道这里发生了什么吗? If you need more details just let me know.如果您需要更多详细信息,请告诉我。

Update:更新:

As suggested in the comments I used another function to get the job done:正如评论中所建议的,我使用了另一个 function 来完成工作:

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
}

And my delteWsihDelegate-function:还有我的 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")
    }
}

The problem is now that that only works every 20-30 times I swipe.现在的问题是,我每刷 20 到 30 次才有效。 Anyone know why this can happens?任何人都知道为什么会发生这种情况?

Update 2更新 2

I found out what the problem was.我发现了问题所在。 I have another GestureRecognizer on the same view.我在同一视图上有另一个GestureRecognizer After deleting that the function works perfectly fine.删除 function 后工作得很好。 Is there any way to have both working at the same time?有没有办法让两者同时工作?

The UITableViewDelegate implements the method UITableViewDelegate实现方法

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

which can be used to configure a swipe action - a delete in your case.可用于配置滑动操作 - 在您的情况下为删除。 Here is a example I use in one of my projects:这是我在一个项目中使用的示例:

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
    }
}

Since your GestureRecognizer on the entire view gives you trouble, I'll recommend using a third party implementation or apples public API to handle dismissing your viewController instead of implementing it yourself.由于您在整个视图上的 GestureRecognizer 会给您带来麻烦,我建议您使用第三方实现或苹果公共 API 来处理关闭您的 viewController 而不是自己实现它。 In that case you can use the UIModalPresentationStyle of your viewController.在这种情况下,您可以使用 viewController 的 UIModalPresentationStyle。

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

I have implemented delete action in the UITableViewController, Use the following code:-我在 UITableViewController 中实现了删除操作,使用以下代码:-

class TestTableViewController: UITableViewController {

    var arrData =  ["test1", "test111", "test11121", "test1121", "test121", "test1123", "test13213", "test165342", ]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return arrData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell
        cell.title.text = arrData[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }


    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            tableView.beginUpdates()
            arrData.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .none)
            tableView.endUpdates()
        }
    }

}

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

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