Попробуйте использовать это вместо тех трех функций, которые вы пробовали:
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
self.array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
return [deleteAction]
}
EDIT
После просмотра вашего скриншота я считаю, что это то, чего вы пытаетесь достичь. Вы пытаетесь включить удаление, когда нажата editButton
, а затем появляется значок удаления. Если это так, пожалуйста, попробуйте этот код.
class TableViewController: UITableViewController {
var array = [1,2,3]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
navigationItem.rightBarButtonItem = editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(array[indexPath.row])"
return cell
}
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//deleting logic
}
}
В противном случае, вы можете рассмотреть возможность реализации только слайда для удаления анимации, и код таков до EDIT .