Ios tableView.reloadData()结束其他动画,如何修复?
当我用右击删除一行时,我正在制作一个具有精美动画的todolist:Ios tableView.reloadData()结束其他动画,如何修复?,ios,swift,uitableview,Ios,Swift,Uitableview,当我用右击删除一行时,我正在制作一个具有精美动画的todolist: func toDoItemDeleted(toDoItem: ToDoItem) { let index = (toDoItems as NSArray).indexOfObject(toDoItem) if index == NSNotFound { return } // could removeAtIndex in the loop but keep it here for when index
func toDoItemDeleted(toDoItem: ToDoItem) {
let index = (toDoItems as NSArray).indexOfObject(toDoItem)
if index == NSNotFound { return }
// could removeAtIndex in the loop but keep it here for when indexOfObject works
toDoItems.removeAtIndex(index)
// use the UITableView to animate the removal of this row
tableView.beginUpdates()
let indexPathForRow = NSIndexPath(forRow: index, inSection: 0)
tableView.deleteRowsAtIndexPaths([indexPathForRow], withRowAnimation: .Fade)
tableView.endUpdates()
// refresh gradient effect of rows
tableView.reloadData()
}
因此,基本上我删除数据,然后使用tableView制作一个remove动画。deleteRowsAtIndexPaths([indexPathForRow],withRowAnimation:.Fade)
,然后我决定通过调用reloadData()修复所有现有行的渐变效果(行的背景色,从红到黄,从下到下,删除一行会破坏渐变效果)
结果是reloadData()
在淡入淡出动画开始之前发生得太快,因此会终止动画。我的问题是:1.为什么?
2.如何解决此问题?我建议您在对希望删除的一行调用deleteRowsAtIndexPaths的同时,也可以对所有其他剩余索引路径调用reloadRowsAtIndexPaths
func reloadRowsAtIndexPaths(_ indexPaths: [NSIndexPath],
withRowAnimation animation: UITableViewRowAnimation)
这将使UITableView的数据源请求cellForRowAtIndexPath用于所有剩余的索引路径
很酷的东西。你需要使用动画块来实现这一点。试试这个:-
UIView.animateWithDuration(0.3, delay: 0,
options: [], animations: {
toDoItems.removeAtIndex(index)
// use the UITableView to animate the removal of this row
tableView.beginUpdates()
let indexPathForRow = NSIndexPath(forRow: index, inSection: 0)
tableView.deleteRowsAtIndexPaths([indexPathForRow], withRowAnimation: .Fade)
tableView.endUpdates()
// refresh gradient effect of rows
}, completion: { _ in
tableView.reloadData()
})
根据需要设置持续时间。(0.3或您想要的值)答案很简单-您正在使用动画删除行,这需要时间,通常约为.3秒,但是,调用
reloadData
会强制表立即重绘其内容,从而停止未完成的动画。你可以
1) 只需等待半秒钟,即可在或类似内容之后使用dispatch\u重新加载您的表
2) 从数据源中删除相应的数据,并使用reloadSections:
withRowAnimation:
不手动删除单元格
3) 或者,离开它,如果它对其他剩余单元格不重要,就不要重新加载表 谢谢,我试过了,动画的某些部分仍然相互重叠。顺便说一句,我仍然很好奇为什么reloadData()
会破坏动画,你想解释一下吗?我已经尝试了tableView.reloadSections(NSIndexSet.init(index:0),其中rowanimation:.None)
,动画仍然彼此重叠。