Skip to content

Instantly share code, notes, and snippets.

@aheze
Last active July 26, 2020 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/2e6ccae2b5421f2adbdcb8970b6644b3 to your computer and use it in GitHub Desktop.
Save aheze/2e6ccae2b5421f2adbdcb8970b6644b3 to your computer and use it in GitHub Desktop.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AlphabetCollectionCell
if collectionView == baseCollectionView {
///We have 2 collectionviews, so we must differentiate.
cell.backgroundColor = .green
cell.nameLabel.text = newBaseCollectionViewArray[indexPath.item].labelName
} else {
cell.backgroundColor = .red
cell.nameLabel.text = secondCollectionViewArray[indexPath.item].labelName
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == baseCollectionView {
///If this collectionview is the second one
return newBaseCollectionViewArray.count
} else { ///If not, this must be the first collection view.
return secondCollectionViewArray.count
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == baseCollectionView {
let alphabetLetter = newBaseCollectionViewArray[indexPath.item]
secondCollectionViewArray.append(alphabetLetter)
let newIndexPath = IndexPath(item: secondCollectionViewArray.count - 1, section: 0)
secondCollectionView.insertItems(at: [newIndexPath])
newBaseCollectionViewArray.remove(at: indexPath.item)
baseCollectionView.deleteItems(at: [indexPath])
} else {
let alphabetLetter = secondCollectionViewArray[indexPath.item]
newBaseCollectionViewArray.insert(alphabetLetter, at: 0)
let newIndexPath = IndexPath(item: 0, section: 0)
///I guess we'll put it at the front for now?
baseCollectionView.insertItems(at: [newIndexPath])
secondCollectionViewArray.remove(at: indexPath.item)
secondCollectionView.deleteItems(at: [indexPath])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment