Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created June 24, 2020 07:39
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 stevencurtis/56b3bc858e66024a0e247cd9576f9eb7 to your computer and use it in GitHub Desktop.
Save stevencurtis/56b3bc858e66024a0e247cd9576f9eb7 to your computer and use it in GitHub Desktop.
collectionviewcontroller
class ViewController: UIViewController {
var collectionView: UICollectionView!
var dta = ["a","kldfkaunfd","jello|", "fourth", "5","a","kldfkaunfd","jello|",
"fourth", "5", "a","kldfkaunfd","jello|", "fourth",
"5","a","kldfkaunfd","jello|", "fourth", "5",
"a","kldfkaunfd","jello|", "fourth", "5", "a","kldfkaunfd",
"jello|", "fourth", "5"]
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 150, height: 150)
layout.scrollDirection = .horizontal
let frame = self.view.frame
collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
let nib = UINib(nibName: "CustomCollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "cell")
self.view.addSubview(collectionView)
collectionView.backgroundColor = .lightGray
self.view.backgroundColor = .lightGray
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
dta.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
cell.txtLab.text = dta[indexPath.row]
return cell
}
}
extension ViewController: UICollectionViewDelegate {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment