0

I'm trying to insert a row after another row deletion animation is completed. I've been trying doing the following:

tableView.beginUpdates()
CATransaction.begin()

CATransaction.setCompletionBlock {
    tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
}

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)

CATransaction.commit()
tableView.endUpdates

This gave me the usual assertion failure when the count of rows is not the same as it's been expecting.

Then I've tried using an UIView animation with a completion block:

tableView.beginUpdates()

func animations() {
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
}

func completion() {
    if count == self.payments.count && self.payments.isEmpty { insert() }
}

UIView.animateWithDuration(0.5, animations: { animations() }) { _ in completion() }

tableView.endUpdates()

Both attempts is giving me the same error. Is it possible or should I look into custom animation for inserting / deleting tableview rows?


Edit:

I managed to make it work by moving tableView.endUpdates() to the completion block. But the insertion animation still animates at the same time when the row is being deleted.

Is there another way of doing this?

Henny Lee
  • 2,970
  • 3
  • 20
  • 37

2 Answers2

0

if you know how much time does your animation takes to complete just add this function to wait for a given amount of time before executing some code:

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

Usage:

delay(seconds: 0.5) { 
    //code to be delayed "0.5 sec"
}
Med Abida
  • 1,214
  • 11
  • 31
0

I think you're putting code in wrong position

It should be like this:

CATransaction.begin()

CATransaction.setCompletionBlock {
  // animation has finished
}

tableView.beginUpdates()
// do some work
tableView endUpdates()

CATransaction.commit()

Reference: How to detect that animation has ended on UITableView beginUpdates/endUpdates?

Community
  • 1
  • 1
Tiep Vu Van
  • 975
  • 8
  • 13