[Swift] Note that you cannot delete only specific cells in TableView

This title

In the process of creating a certain function, I have the beginning (0th) of the array by default, and I will write down the method I used when I wanted to make it impossible to delete that value. The content is simple.

manner

only this. Set the editing style of each cell with the delegate method of UITableViewDelegate. This time, by applying none to the 0th and applying delete to other than that, only the 0th cannot be deleted.

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    indexPath.row == 0 ? .none : .delete
}

Since return can be omitted if swift has one line in {}, it is better to write it using the ternary operator. By the way, if you write it in an if statement like this.

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    if indexPath.row == 0 {
        return .none
    }
    return .delete
}

that's all.

Recommended Posts

[Swift] Note that you cannot delete only specific cells in TableView
Note that system properties including JAXBContext cannot be used in Java11