Last active
February 27, 2020 01:07
-
-
Save aheze/e3032121690228de8b07a09fec8b4af4 to your computer and use it in GitHub Desktop.
This file contains 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
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { | |
///1. | |
var baseCollectionViewArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] | |
var secondCollectionViewArray = [String]() | |
let baseCollectionView = UICollectionView(frame: CGRect(x: 25, y: 50, width: 300, height: 200), collectionViewLayout: UICollectionViewFlowLayout()) | |
let secondCollectionView = UICollectionView(frame: CGRect(x: 25, y: 300, width: 300, height: 200), collectionViewLayout: UICollectionViewFlowLayout()) | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
///2. | |
baseCollectionView.register(AlphabetCollectionCell.self, forCellWithReuseIdentifier: "Cell") | |
baseCollectionView.delegate = self | |
baseCollectionView.dataSource = self | |
secondCollectionView.register(AlphabetCollectionCell.self, forCellWithReuseIdentifier: "Cell") | |
secondCollectionView.delegate = self | |
secondCollectionView.dataSource = self | |
///3. | |
view.addSubview(baseCollectionView) | |
view.addSubview(secondCollectionView) | |
self.view = view | |
} | |
///4. | |
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 = baseCollectionViewArray[indexPath.item] | |
} else { | |
cell.backgroundColor = .red | |
cell.nameLabel.text = secondCollectionViewArray[indexPath.item] | |
} | |
return cell | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
if collectionView == baseCollectionView { | |
///If this collectionview is the second one | |
return baseCollectionViewArray.count | |
} else { ///If not, this must be the first collection view. | |
return secondCollectionViewArray.count | |
} | |
} | |
///5. | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
if collectionView == baseCollectionView { | |
let alphabetLetter = baseCollectionViewArray[indexPath.item] | |
secondCollectionViewArray.append(alphabetLetter) | |
let newIndexPath = IndexPath(item: secondCollectionViewArray.count - 1, section: 0) | |
secondCollectionView.insertItems(at: [newIndexPath]) | |
baseCollectionViewArray.remove(at: indexPath.item) | |
baseCollectionView.deleteItems(at: [indexPath]) | |
} else { | |
///We'll do this later | |
} | |
} | |
} | |
///6. | |
class AlphabetCollectionCell: UICollectionViewCell { | |
var nameLabel: UILabel = UILabel(frame: CGRect.zero) | |
override init(frame : CGRect) { | |
super.init(frame : frame) | |
///Set up the label that shows the letter | |
nameLabel.frame.size = CGSize(width: self.frame.width, height: self.frame.height) | |
nameLabel.textAlignment = .center | |
contentView.addSubview(nameLabel) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment