0
I have a tableView where the user can delete the cells he selects. In my case the first cell of the tableView cannot be deleted by the user. How do I make only the first cell of the tableView not editable?
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var users = ["Todos", "User1", "User2", "User3", "User5"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = user.name
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
self.users.remove(at: indexPath.row)
}
}
}