Ios developer
Ios developer

Reputation: 54

Animate tableview cell when deleting

I have a tableview. The tableview cell contains images, labels. In this cell I have a button to delete the row. I want to animate the cell during deletion(ex. tinder like left or right swipe) or any animation to make the app actractive. How to do this type of animation. Or any recommended library ?

Upvotes: 0

Views: 845

Answers (2)

Nahid Raihan
Nahid Raihan

Reputation: 1017

Typical Animation for your case :

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        // your code

        tableView.deleteRows(at: [indexPath], with: .automatic)

       // your code

    }
}

Alternate Way for custom Animation (Updated):

tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .top) // Check (.)after for available animation example comment   out below -
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .left)
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .right)
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .up)
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .down)
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .fade)
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .middle)
//tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .bottom)
tableView.endUpdates()

Upvotes: 1

pigeon_39
pigeon_39

Reputation: 1513

UITableViewCellEditingStyle will do the job for you for i.e

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        //do your necessary operations here
        //reload your UITableView here
    }
}

Swipe right to left to see the animation.

Upvotes: 1

Related Questions