Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReloadRowsAtIndexPaths with no Row Animation animates

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?

like image 502
HansiOber Avatar asked Oct 01 '15 20:10

HansiOber


2 Answers

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.

like image 188
Charles A. Avatar answered Nov 12 '22 18:11

Charles A.


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.

like image 36
Alec O Avatar answered Nov 12 '22 18:11

Alec O