没有行动画动画的 ReloadRowsAtIndexPaths
ReloadRowsAtIndexPaths with no Row Animation animates
这段代码的问题(在 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)
}
是 reloadRowsAtIndexPaths 都是动画的,尽管指定了 withRowAnimation: .None。我在这里错过了什么?
在 iOS
和 OS X
中,您经常会出于某种原因以隐式动画结束(例如,代码正在被其他已经执行的代码执行)触发动画)。
根据我的经验,使用 UITableView
和 UICollectionView
控制动画特别困难。您最好的选择可能是将对 reloadRowsAtIndexPaths:withRowAnimation:
的调用放入传递给 UIView
的 performWithoutAnimations:
方法的闭包中:
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)
}
}
注意:在同时发生的多个更新之前和之后调用 tableView.beginUpdates()
和 tableView.endUpdates()
也是一个不错的主意。
虽然 Charles 描述了这个问题的适当解决方案,但它没有回答为什么 UITableViewRowAnimation.none
提供动画的问题。
根据 UITableViewRowAnimation.none
的文档,"The inserted or deleted rows use the default animations." 因此,虽然 .none
听起来不应该有动画,但它实际上更像 .automatic
。
我会把它留作评论,但评论不能包含图片。请注意,这个答案并不意味着解决问题,因为查尔斯已经这样做了。仅供下一个想知道为什么UITableViewRowAnimation.none
提供动画的人参考。
这段代码的问题(在 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)
}
是 reloadRowsAtIndexPaths 都是动画的,尽管指定了 withRowAnimation: .None。我在这里错过了什么?
在 iOS
和 OS X
中,您经常会出于某种原因以隐式动画结束(例如,代码正在被其他已经执行的代码执行)触发动画)。
根据我的经验,使用 UITableView
和 UICollectionView
控制动画特别困难。您最好的选择可能是将对 reloadRowsAtIndexPaths:withRowAnimation:
的调用放入传递给 UIView
的 performWithoutAnimations:
方法的闭包中:
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)
}
}
注意:在同时发生的多个更新之前和之后调用 tableView.beginUpdates()
和 tableView.endUpdates()
也是一个不错的主意。
虽然 Charles 描述了这个问题的适当解决方案,但它没有回答为什么 UITableViewRowAnimation.none
提供动画的问题。
根据 UITableViewRowAnimation.none
的文档,"The inserted or deleted rows use the default animations." 因此,虽然 .none
听起来不应该有动画,但它实际上更像 .automatic
。
我会把它留作评论,但评论不能包含图片。请注意,这个答案并不意味着解决问题,因为查尔斯已经这样做了。仅供下一个想知道为什么UITableViewRowAnimation.none
提供动画的人参考。