I want to delete a table row by action from button in custom cell, using native tableView animations. Can someone help me please?
Asked
Active
Viewed 885 times
-2
-
Hi Renato and welcome to stack overflow. Could you please explain better what you want to do and show what code you have so far. – boidkan Oct 17 '17 at 17:00
-
Search for delete cell tableview animation – J. Doe Oct 17 '17 at 17:05
-
1Related: https://stackoverflow.com/questions/40156274/deleting-a-row-from-a-uitableview-in-swift-3 – Ahmad F Oct 17 '17 at 17:08
1 Answers
0
I Found a good solution, using a custom delegate and TableViewDataSource method!
I had to work with Protocol in my custom cell Class, like this:
protocol MyCellDelegate: class {
func didDelete(cell: MyCell)
}
In my custom cell Class I made this:
class MyCell: UITableViewCell {
weak var delegate: MyCellDelegate?
@IBAction func deleteButtonTapped(_ sender: Any) {
delegate?.didDelete(cell: self)
}
}
In my ViewController I made the delegates and data source implementation:
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
tableView.beginUpdates()
tableView.deleteRows(at:[indexPath], with: .fade)
tableView.endUpdates()
default:
break
}
}
}
extension ViewController: MyCellDelegate {
func didDelete(cell: MyCell) {
guard let index = tableView.indexPath(for: cell) else {
return
}
myArray?.remove(at: index.row)
self.tableView(self.tableView, commit: .delete, forRowAt: index)
}
}

Renato Machado Filho
- 31
- 2