UITableView disable swipe to delete for particular cells swift

You might customize the UITableViewDelegate's function editingStyleForRowAt, especially returning UITableViewCellEditingStyle.none when you don't need the swipe, something like:

public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
   if mainList[indexPath.row].statusId == "12" {
        return UITableViewCellEditingStyle.none
    } else {
        return UITableViewCellEditingStyle.delete
    }
}

Use this table view delegate method

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

}

Documentation tableView:canEditRowAtIndexPath:


Thanks for posting this question and the answers above.

On an expansion of the answers, I had a default row that should never be deleted, so I set a tag on that particular cell and then at first used this method.

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if tableView.cellForRow(at: indexPath)?.tag == 100 {
        return false
    }
    return true
}

HOWEVER - This caused the following warning in Debugger -

Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed.

So, as other commentators have said, use this method below:

Swift 5.0

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    if tableView.cellForRow(at: indexPath)?.tag == 100 {
        return UITableViewCell.EditingStyle.none
    } else {
        return UITableViewCell.EditingStyle.delete
    }
}