Last active
May 26, 2017 18:43
-
-
Save GottaGetSwifty/414897cfeba33a8ea8edb51239a07612 to your computer and use it in GitHub Desktop.
Example for CollectionView's didSelectItemAt with single selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){ | |
| // First selection, just set it and reload that cell | |
| guard let selectedIndexPath = selectedIndexPath else { | |
| selectedIndexPath = indexPath | |
| collectionView.reloadItems(at: [indexPath]) | |
| return | |
| } | |
| // Reset to none selected and reload that cell | |
| if(selectedIndexPath == indexPath) { | |
| selectedIndexPath = nil | |
| collectionView.reloadItems(at: [indexPath]) | |
| } | |
| // Set the new selected index and reload the previous and new cells. | |
| else { | |
| let indexPathsToRefresh = [indexPath, selectedIndexPath] | |
| selectedIndexPath = indexPath | |
| collectionView.reloadItems(at: [indexPathsToRefresh]) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment