Asif Raza
Asif Raza

Reputation: 1012

How to disable swipe to delete option at UITableView

I have 2 UITableView (Table-1 & Table-2) at same UIViewController. I want to editing functionality in my Table-2.

I have added tableview datasource method in my view controller as mentioned below: -

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {

            tableView.beginUpdates()
            myArray.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            tableView.endUpdates()
        }
    } 

and both tableview calling this method. So each tableview cells are able to open delete option.

But I want this delete editing option in only Table-2. I want to restrict delete editing option functionality in Table-1.

Please help. Thanks in advance.

Upvotes: 0

Views: 1044

Answers (1)

gm_
gm_

Reputation: 617

You can use this function provided by UITableViewDelegate:

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

Simply implement some logic to check if you want to allow editing at the given indexPath and given tableView and return true if you want editing/deleting and return false if you don't.

Hope this helps!

Upvotes: 1

Related Questions