【问题标题】:ReloadRowsAtIndexPaths with no Row Animation animates没有行动画动画的 ReloadRowsAtIndexPaths
【发布时间】:2015-12-30 00:32:08
【问题描述】:

这段代码的问题(里面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 是指定的。我在这里错过了什么?

【问题讨论】:

    标签: uitableview swift2 xcode7


    【解决方案1】:

    iOSOS X 中,由于某种原因(例如,代码正在由已经触发动画的其他代码执行),您最终会得到一个隐式动画。

    p>

    根据我的经验,使用UITableViewUICollectionView 控制动画可能特别困难。您最好的选择可能是将对reloadRowsAtIndexPaths:withRowAnimation: 的调用放入传递给UIViewperformWithoutAnimations: 方法的闭包中:

    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() 也不错。

    【讨论】:

    • 但是 tableView.beginUpdates() 和 tableView.endUpdates() 成功了!即使没有 UIView.performWithoutAnimation。谢谢!
    • performWithoutAnimations 为我解决了问题。不需要beginUpdatesendUpdates 调用。
    【解决方案2】:

    虽然 Charles 描述了针对此问题的适当解决方案,但它没有回答为什么 UITableViewRowAnimation.none 提供动画的问题。

    根据UITableViewRowAnimation.none 的文档,“插入或删除的行使用默认动画。”因此,虽然.none 听起来不应该有动画,但它实际上更像.automatic

    我会将此作为评论,但 cmets 不能包含图片。请注意,这个答案并不是为了解决问题,因为查尔斯已经这样做了。这仅供下一个想知道为什么UITableViewRowAnimation.none 提供动画的人参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-24
      • 2021-07-03
      • 1970-01-01
      • 2011-06-01
      • 2013-09-30
      • 1970-01-01
      • 2017-06-21
      • 2020-05-19
      相关资源
      最近更新 更多