Chris
Chris

Reputation: 2274

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:

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. However I can click a Button inside a tableViewCell so I don't think its blocking anything.

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:

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:

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. Anyone know why this can happens?

Update 2

I found out what the problem was. I have another GestureRecognizer on the same view. After deleting that the function works perfectly fine. Is there any way to have both working at the same time?

Upvotes: 1

Views: 3332

Answers (2)

mflknr
mflknr

Reputation: 130

The UITableViewDelegate implements the method

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. In that case you can use the UIModalPresentationStyle of your viewController.

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

Upvotes: 2

Ashutosh Mishra
Ashutosh Mishra

Reputation: 1939

I have implemented delete action in the UITableViewController, Use the following code:-

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

}

Upvotes: 0

Related Questions