I\'m using the code below to delete a row in my tableview. First I delete the object from my array and then from the tableview using this code:
let i = IndexPath
Problem
The problem in you case is the Tag you are setting on the Button , button Tag is not changed when you are deleting the Rows , as its the All Cell is not Reloaded.
i guess you are setting tag in cellforRowIndexpath
you try with putting tag on button in tableWillDisplayCell
or
the best way to do this is below .
Solution
you can get the IndexPath
in other way
Suppose if your view hierarchy is
UITableViewCell-> contentView -> button
in you button click method
if let cell = sender.superview?.superview as? UITableviewCell{
let indexPath = myTableView.indexPath(for: cell)
// Now delete the table cell
myArray.remove(at: indexPath.row)
myTableView.beginUpdates()
myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left)
myTableView.endUpdates()
}
i hope this will work for you .