Advertisement
Don_Mag

Untitled

Jul 11th, 2023 (edited)
1,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.03 KB | None | 0 0
  1. // basic cell with centered label
  2. class QuickCell: UICollectionViewCell {
  3.     let label = UILabel()
  4.     override init(frame: CGRect) {
  5.         super.init(frame: frame)
  6.         commonInit()
  7.     }
  8.     required init?(coder: NSCoder) {
  9.         super.init(coder: coder)
  10.         commonInit()
  11.     }
  12.     func commonInit() {
  13.         label.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
  14.         label.translatesAutoresizingMaskIntoConstraints = false
  15.         contentView.addSubview(label)
  16.         NSLayoutConstraint.activate([
  17.             label.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
  18.             label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
  19.         ])
  20.     }
  21. }
  22.  
  23. // tap anywhere outside the collection view to add 2-to-7 new items
  24. // tap any cell to reset to Zero items
  25. class QuickTestVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  26.    
  27.     var photos: [Int] = []
  28.    
  29.     var collectionView: UICollectionView!
  30.  
  31.     let colors: [UIColor] = [
  32.         .red, .green, .blue, .cyan, .yellow, .magenta,
  33.     ]
  34.     override func viewDidLoad() {
  35.         super.viewDidLoad()
  36.        
  37.         let fl = UICollectionViewFlowLayout()
  38.         fl.itemSize = .init(width: 80.0, height: 40.0)
  39.         collectionView = UICollectionView(frame: .zero, collectionViewLayout: fl)
  40.         collectionView.backgroundColor = .systemYellow
  41.         collectionView.translatesAutoresizingMaskIntoConstraints = false
  42.         view.addSubview(collectionView)
  43.         let g = view.safeAreaLayoutGuide
  44.         NSLayoutConstraint.activate([
  45.             collectionView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  46.             collectionView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  47.             collectionView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  48.             collectionView.heightAnchor.constraint(equalToConstant: 300.0),
  49.         ])
  50.        
  51.         collectionView.register(QuickCell.self, forCellWithReuseIdentifier: "c")
  52.         collectionView.dataSource = self
  53.         collectionView.delegate = self
  54.     }
  55.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  56.         let startVal: Int = self.photos.count
  57.         let newPhotos: [Int] = Array(startVal..<startVal+3)
  58.         add(photos: newPhotos)
  59.     }
  60.     func add(photos: [Int]) {
  61.         let previousCount = self.photos.count
  62.         self.photos.append(contentsOf: photos)
  63.        
  64.         let indexPaths = (0..<photos.count).map { IndexPath(item: previousCount +  $0, section: 0) }
  65.         collectionView.insertItems(at: indexPaths)
  66.     }
  67.    
  68.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  69.         print("numberOfItemsInSection:", self.photos.count)
  70.         return self.photos.count
  71.     }
  72.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  73.         let c = collectionView.dequeueReusableCell(withReuseIdentifier: "c", for: indexPath) as! QuickCell
  74.         c.label.text = "\(self.photos[indexPath.item])"
  75.         c.contentView.backgroundColor = colors[indexPath.item % colors.count]
  76.         return c
  77.     }
  78.     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  79.         self.photos = []
  80.         collectionView.reloadData()
  81.     }
  82.    
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement