Skip to content

Instantly share code, notes, and snippets.

@GottaGetSwifty
Last active May 26, 2017 18:43
Show Gist options
  • Select an option

  • Save GottaGetSwifty/414897cfeba33a8ea8edb51239a07612 to your computer and use it in GitHub Desktop.

Select an option

Save GottaGetSwifty/414897cfeba33a8ea8edb51239a07612 to your computer and use it in GitHub Desktop.
Example for CollectionView's didSelectItemAt with single selection
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