Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.76 KB | None | 0 0
  1. private let reuseIdentifier = "MyCollectionViewCell"
  2.  
  3. struct CarBrand {
  4.     var brandName: String
  5.     var imageView: UIImageView
  6. }
  7.  
  8. class MyCollectionViewController: UICollectionViewController {
  9.    
  10.     fileprivate let sectionInsets = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)
  11.     fileprivate var cells = [CarBrand]()
  12.    
  13.     fileprivate let heigthForBrandLabel:CGFloat = 10
  14.     fileprivate let itemsPerRow: CGFloat = 2
  15.    
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.         cells = fillCollectionViewData()
  19.         collectionView?.reloadData()
  20.         //self.collectionView?.reloadData()
  21.         // Uncomment the following line to preserve selection between presentations
  22.         // self.clearsSelectionOnViewWillAppear = false
  23.  
  24.         // Register cell classes
  25.         //self.collectionView!.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  26.  
  27.         // Do any additional setup after loading the view.
  28.     }
  29.  
  30.  
  31.     // MARK: UICollectionViewDelegate
  32.  
  33.     /*
  34.     // Uncomment this method to specify if the specified item should be highlighted during tracking
  35.     override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
  36.         return true
  37.     }
  38.     */
  39.  
  40.     /*
  41.     // Uncomment this method to specify if the specified item should be selected
  42.     override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
  43.         return true
  44.     }
  45.     */
  46.  
  47.     /*
  48.     // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
  49.     override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
  50.         return false
  51.     }
  52.  
  53.     override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
  54.         return false
  55.     }
  56.  
  57.     override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
  58.    
  59.     }
  60.     */
  61.  
  62. }
  63. // MARK: Private functions
  64.  
  65. extension MyCollectionViewController {
  66.     fileprivate func fillCollectionViewData() -> [CarBrand] {
  67.         var newCells = [CarBrand]()
  68.        
  69.         for _ in 0..<6 {
  70.             let imageView = UIImageView(image: UIImage(named: "bitcoin-gold.png"))
  71.             let car = CarBrand(brandName: "default", imageView: imageView)
  72.             newCells.append(car)
  73.         }
  74.         for i in 0..<6 {
  75.             switch i {
  76.             case 0:
  77.                 newCells[i].brandName = "BMW"
  78.                 newCells[i].imageView.image = UIImage(named: "0.png")
  79.             case 1:
  80.                 newCells[i].brandName = "Audi"
  81.                 newCells[i].imageView.image = UIImage(named: "1.jpg")
  82.             case 2:
  83.                 newCells[i].brandName = "Mercedes Benz"
  84.                 newCells[i].imageView.image = UIImage(named: "2.jpg")
  85.             case 3:
  86.                 newCells[i].brandName = "Volkswagen"
  87.                 newCells[i].imageView.image = UIImage(named: "3.jpg")
  88.             case 4:
  89.                 newCells[i].brandName = "Skoda"
  90.                 newCells[i].imageView.image = UIImage(named: "4.png")
  91.             case 5:
  92.                 newCells[i].brandName = "Fiat"
  93.                 newCells[i].imageView.image = UIImage(named: "5.jpg")
  94.             default:
  95.                 break
  96.             }
  97.         }
  98.         return newCells
  99.     }
  100. }
  101. // MARK: UICollectionViewDataSource
  102. extension MyCollectionViewController {
  103.     override func numberOfSections(in collectionView: UICollectionView) -> Int {
  104.         return 1
  105.     }
  106.    
  107.    
  108.     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  109.         // #warning Incomplete implementation, return the number of items
  110.         return cells.count
  111.     }
  112.    
  113.     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  114.         let car = cells[indexPath.row]
  115.         guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? MyCollectionViewCell else {
  116.             fatalError("The dequeued cell is not an instance of \(reuseIdentifier).")
  117.         }
  118.         cell.brandNameLabel.text = car.brandName
  119.         cell.brandImageView.image = car.imageView.image
  120.         return cell
  121.     }
  122. }
  123.  
  124. // MARK: UICollectionViewDelegateFlowLayout
  125.  
  126. extension MyCollectionViewController : UICollectionViewDelegateFlowLayout {
  127.     func collectionView(_ collectionView: UICollectionView,
  128.                         layout collectionViewLayout: UICollectionViewLayout,
  129.                         sizeForItemAt indexPath: IndexPath) -> CGSize {
  130.  
  131.         let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
  132.         let availableWidth = view.frame.width - paddingSpace
  133.         let widthPerItem = availableWidth / itemsPerRow
  134.         let heigthPerItem = widthPerItem + heigthForBrandLabel
  135.        
  136.         return CGSize(width: widthPerItem, height: heigthPerItem)
  137.     }
  138.    
  139.     func collectionView(_ collectionView: UICollectionView,
  140.                         layout collectionViewLayout: UICollectionViewLayout,
  141.                         insetForSectionAt section: Int) -> UIEdgeInsets {
  142.         return sectionInsets
  143.     }
  144.    
  145.     // 4
  146.     func collectionView(_ collectionView: UICollectionView,
  147.                         layout collectionViewLayout: UICollectionViewLayout,
  148.                         minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  149.         return sectionInsets.left
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement