Created
July 2, 2020 03:31
-
-
Save bob910078/86b6b4fdb34003919d0480b3ddaf10c7 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
class ViewController: UIViewController { | |
var viewModel: [String] = [] | |
lazy var collectionView: UICollectionView = { | |
let layout = UICollectionViewFlowLayout() | |
layout.estimatedItemSize = CGSize(width: 1, height: 1) | |
let view = UICollectionView(frame: .zero, collectionViewLayout: layout) | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.register(LabelCollectionViewCell.self, forCellWithReuseIdentifier: "Cell") | |
view.dataSource = self | |
view.backgroundColor = .clear | |
return view | |
}() | |
private lazy var collectionViewConstraints: [NSLayoutConstraint] = [ | |
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | |
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), | |
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor), | |
] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
setupLayoutConstraints() | |
} | |
private func setupLayoutConstraints() { | |
view.addSubview(collectionView) | |
NSLayoutConstraint.activate(collectionViewConstraints) | |
} | |
} | |
extension ViewController: UICollectionViewDataSource { | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return viewModel.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! LabelCollectionViewCell | |
cell.label.text = viewModel[indexPath.item] | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment