Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private let reuseIdentifier = "MyCollectionViewCell"
- struct CarBrand {
- var brandName: String
- var imageView: UIImageView
- }
- class MyCollectionViewController: UICollectionViewController {
- fileprivate let sectionInsets = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)
- fileprivate var cells = [CarBrand]()
- fileprivate let heigthForBrandLabel:CGFloat = 10
- fileprivate let itemsPerRow: CGFloat = 2
- override func viewDidLoad() {
- super.viewDidLoad()
- cells = fillCollectionViewData()
- collectionView?.reloadData()
- //self.collectionView?.reloadData()
- // Uncomment the following line to preserve selection between presentations
- // self.clearsSelectionOnViewWillAppear = false
- // Register cell classes
- //self.collectionView!.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
- // Do any additional setup after loading the view.
- }
- // MARK: UICollectionViewDelegate
- /*
- // Uncomment this method to specify if the specified item should be highlighted during tracking
- override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
- return true
- }
- */
- /*
- // Uncomment this method to specify if the specified item should be selected
- override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
- return true
- }
- */
- /*
- // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
- return false
- }
- override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
- return false
- }
- override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
- }
- */
- }
- // MARK: Private functions
- extension MyCollectionViewController {
- fileprivate func fillCollectionViewData() -> [CarBrand] {
- var newCells = [CarBrand]()
- for _ in 0..<6 {
- let imageView = UIImageView(image: UIImage(named: "bitcoin-gold.png"))
- let car = CarBrand(brandName: "default", imageView: imageView)
- newCells.append(car)
- }
- for i in 0..<6 {
- switch i {
- case 0:
- newCells[i].brandName = "BMW"
- newCells[i].imageView.image = UIImage(named: "0.png")
- case 1:
- newCells[i].brandName = "Audi"
- newCells[i].imageView.image = UIImage(named: "1.jpg")
- case 2:
- newCells[i].brandName = "Mercedes Benz"
- newCells[i].imageView.image = UIImage(named: "2.jpg")
- case 3:
- newCells[i].brandName = "Volkswagen"
- newCells[i].imageView.image = UIImage(named: "3.jpg")
- case 4:
- newCells[i].brandName = "Skoda"
- newCells[i].imageView.image = UIImage(named: "4.png")
- case 5:
- newCells[i].brandName = "Fiat"
- newCells[i].imageView.image = UIImage(named: "5.jpg")
- default:
- break
- }
- }
- return newCells
- }
- }
- // MARK: UICollectionViewDataSource
- extension MyCollectionViewController {
- override func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of items
- return cells.count
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let car = cells[indexPath.row]
- guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? MyCollectionViewCell else {
- fatalError("The dequeued cell is not an instance of \(reuseIdentifier).")
- }
- cell.brandNameLabel.text = car.brandName
- cell.brandImageView.image = car.imageView.image
- return cell
- }
- }
- // MARK: UICollectionViewDelegateFlowLayout
- extension MyCollectionViewController : UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- sizeForItemAt indexPath: IndexPath) -> CGSize {
- let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
- let availableWidth = view.frame.width - paddingSpace
- let widthPerItem = availableWidth / itemsPerRow
- let heigthPerItem = widthPerItem + heigthForBrandLabel
- return CGSize(width: widthPerItem, height: heigthPerItem)
- }
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- insetForSectionAt section: Int) -> UIEdgeInsets {
- return sectionInsets
- }
- // 4
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return sectionInsets.left
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement