Guest User

Untitled

a guest
Sep 27th, 2023
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.03 KB | Help | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  4.  
  5.     let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
  6.  
  7.     override func viewDidLoad() {
  8.         super.viewDidLoad()
  9.  
  10.         collectionView.dataSource = self
  11.         collectionView.delegate = self
  12.         collectionView.translatesAutoresizingMaskIntoConstraints = false
  13.         collectionView.backgroundColor = .white
  14.         collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
  15.  
  16.         view.addSubview(collectionView)
  17.         NSLayoutConstraint.activate([
  18.             collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  19.             collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  20.             collectionView.topAnchor.constraint(equalTo: view.topAnchor),
  21.             collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  22.         ])
  23.  
  24.         DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in
  25.             self?.collectionView.reloadData()
  26.             print("collection reloaded")
  27.         }
  28.     }
  29.  
  30.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  31.         return 10
  32.     }
  33.  
  34.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  35.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCollectionViewCell
  36.         cell.setup(value: indexPath.item)
  37.  
  38.         return cell
  39.     }
  40. }
  41.  
  42. class MyCollectionViewCell: UICollectionViewCell {
  43.     let moreButton: UIButton!
  44.     var value: Int = -1
  45.  
  46.     override init(frame: CGRect) {
  47.         moreButton = UIButton()
  48.         super.init(frame: frame)
  49.  
  50.         moreButton.setTitle("More", for: .normal)
  51.         moreButton.menu = UIMenu(
  52.             title: "More Menu",
  53.             image: nil,
  54.             identifier: nil,
  55.             options: [],
  56.             children: [
  57.                 UIAction(title: "Share") { [weak self] _ in
  58.                     guard let self else { return }
  59.                     print("Share \(self.value)")
  60.                 }
  61.             ]
  62.         )
  63.         moreButton.showsMenuAsPrimaryAction = true
  64.         backgroundColor = UIColor.lightGray
  65.  
  66.         addSubview(moreButton)
  67.         moreButton.translatesAutoresizingMaskIntoConstraints = false
  68.         NSLayoutConstraint.activate([
  69.             moreButton.topAnchor.constraint(equalTo: topAnchor),
  70.             moreButton.bottomAnchor.constraint(equalTo: bottomAnchor),
  71.             moreButton.leadingAnchor.constraint(equalTo: leadingAnchor),
  72.             trailingAnchor.constraint(equalTo: moreButton.trailingAnchor)
  73.         ])
  74.     }
  75.  
  76.     func setup(value: Int) {
  77.         moreButton.setTitle("\(value)", for: .normal)
  78.         self.value = value
  79.     }
  80.  
  81.     required init?(coder aDecoder: NSCoder) {
  82.         fatalError("init(coder:) has not been implemented")
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment