17

The problem with this code (inside func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath))

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

if indexPath.row > 1{
    tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row-1, inSection: 0)], withRowAnimation: .None)
}
if tDate[activeRow].count == 0{
    tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .None)
}

is that both reloadRowsAtIndexPaths are animated, although withRowAnimation: .None is specified. What am I missing here?

2 Answers 2

27

It is often the case in iOS and OS X that you end up with an implicit animation for one reason or another (for example, the code is being executed by other code that has already triggered animation).

It can be especially difficult to control animations with UITableView and UICollectionView in my experience. Your best bet is probably to put the calls to reloadRowsAtIndexPaths:withRowAnimation: into a closure passed to UIView's performWithoutAnimations: method:

UIView.performWithoutAnimation {
    if indexPath.row > 1{
        tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row-1, inSection: 0)], withRowAnimation: .None)
    }
    if tDate[activeRow].count == 0{
        tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .None)
    }
}

Note: It's also not a bad idea to call tableView.beginUpdates() and tableView.endUpdates() before and after multiple updates that happen all at once.

2
  • But tableView.beginUpdates() and tableView.endUpdates() did the trick! Even without UIView.performWithoutAnimation. Thank You!
    – HansiOber
    Oct 1, 2015 at 21:03
  • 2
    performWithoutAnimations did the trick for me. No beginUpdates or endUpdates calls were needed.
    – marosoaie
    Apr 21, 2017 at 9:46
20

While an appropriate solution for this problem was described by Charles, it did not answer the question as to why UITableViewRowAnimation.none provides an animation.

According to the docs for UITableViewRowAnimation.none, "The inserted or deleted rows use the default animations." Therefore, while .none sounds like there shouldn't be an animation, it actually acts more like .automatic.

enter image description here

I would have left this as a comment, but comments cannot contain pictures. Please note that this answer is not meant to solve the problem as Charles has already done that. This is strictly for reference for the next person wondering why UITableViewRowAnimation.none provides an animation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.