Skip to content

Instantly share code, notes, and snippets.

@ateliercw
Last active June 10, 2016 20:11
Show Gist options
  • Save ateliercw/23f61d6f788b29b61b573e12e3f35bff to your computer and use it in GitHub Desktop.
Save ateliercw/23f61d6f788b29b61b573e12e3f35bff to your computer and use it in GitHub Desktop.
Comparing a curried and un-curried deselect rows function
extension UIViewController {
func rz_smoothlyDeselectRows(tableView tableViewIn: UITableView?) {
guard let tableView = tableViewIn,
selectedIndexPaths = tableView.indexPathsForSelectedRows else {
return
}
let deselect = { (tableView: UITableView, animated: Bool) -> ((NSIndexPath) -> ()) in
return { indexPath in
tableView.deselectRowAtIndexPath(indexPath, animated: animated)
}
}
let reselect = { (tableView: UITableView, animated: Bool) -> ((NSIndexPath) -> ()) in
return { indexPath in
tableView.selectRowAtIndexPath(indexPath, animated: animated, scrollPosition: .None)
}
}
if let coordinator = transitionCoordinator() {
let animation = { (context: UIViewControllerTransitionCoordinatorContext) in
selectedIndexPaths.forEach(deselect(tableView, context.isAnimated()))
}
let completion = { (context: UIViewControllerTransitionCoordinatorContext) in
if context.isCancelled() {
selectedIndexPaths.forEach(reselect(tableView, false))
}
}
let parentView = parentViewController?.view
coordinator.animateAlongsideTransitionInView(parentView, animation: animation, completion: completion)
}
else {
selectedIndexPaths.forEach(deselect(tableView, false))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment