Skip to content

Instantly share code, notes, and snippets.

@sukov
Last active June 30, 2019 11:59
Show Gist options
  • Save sukov/2dc741f3368cb3799c82e30ba2630ccb to your computer and use it in GitHub Desktop.
Save sukov/2dc741f3368cb3799c82e30ba2630ccb to your computer and use it in GitHub Desktop.
DeselectRowsAlongsideTransition Swift
extension UITableView {
func reloadDataAnimated() {
UIView.transition(with: self,
duration: 0.35,
options: .transitionCrossDissolve,
animations: {
self.reloadData()
})
}
/**
Animates cell deselection alongside transition. Must be called on `viewWillAppear`
- Parameters:
- viewController: The UIViewController where the table view is located
*/
func deselectRowsAlongsideTransition(from viewController: UIViewController) {
guard let coordinator = viewController.transitionCoordinator else {
indexPathsForSelectedRows?.forEach { deselectRow(at: $0, animated: false) }
return
}
coordinator.animateAlongsideTransition(in: self,
animation: { [weak self] context in
self?.indexPathsForSelectedRows?.forEach {
self?.deselectRow(at: $0, animated: context.isAnimated)
}
}, completion: { [weak self] context in
if context.isCancelled {
self?.indexPathsForSelectedRows?.forEach {
self?.selectRow(at: $0, animated: false, scrollPosition: .none)
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment