Don_Mag

Collection view in Table View background view

Mar 6th, 2022
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.08 KB | None | 0 0
  1.  
  2. class BkgTableVC: UITableViewController {
  3.    
  4.     let bkgView = TVBkgView()
  5.    
  6.     override func viewDidLoad() {
  7.         super.viewDidLoad()
  8.        
  9.         view.backgroundColor = .white
  10.        
  11.         tableView.register(UITableViewCell.self, forCellReuseIdentifier: "c")
  12.        
  13.         tableView.contentInset = UIEdgeInsets(top: 200, left: 0, bottom: 0, right: 0)
  14.        
  15.         tableView.backgroundView = bkgView
  16.     }
  17.    
  18.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  19.         return 30
  20.     }
  21.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  22.         let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath)
  23.         c.textLabel?.text = "\(indexPath)"
  24.         return c
  25.     }
  26.    
  27. }
  28.  
  29. class TVBkgView: UIView, UICollectionViewDataSource, UICollectionViewDelegate {
  30.    
  31.     let titleLabel: UILabel = {
  32.         let v = UILabel()
  33.         v.backgroundColor = .brown
  34.         v.textColor = .yellow
  35.         v.textAlignment = .center
  36.         return v
  37.     }()
  38.    
  39.     let infoLabel: UILabel = {
  40.         let v = UILabel()
  41.         v.backgroundColor = .brown
  42.         v.textColor = .yellow
  43.         v.textAlignment = .center
  44.         return v
  45.     }()
  46.  
  47.     var cView: UICollectionView!
  48.    
  49.     override init(frame: CGRect) {
  50.         super.init(frame: frame)
  51.         commonInit()
  52.     }
  53.     required init?(coder: NSCoder) {
  54.         super.init(coder: coder)
  55.         commonInit()
  56.     }
  57.     func commonInit() -> Void {
  58.         backgroundColor = .orange
  59.        
  60.         let fl = UICollectionViewFlowLayout()
  61.         fl.scrollDirection = .horizontal
  62.         fl.itemSize = CGSize(width: 160, height: 80)
  63.        
  64.         cView = UICollectionView(frame: .zero, collectionViewLayout: fl)
  65.        
  66.         titleLabel.translatesAutoresizingMaskIntoConstraints = false
  67.         cView.translatesAutoresizingMaskIntoConstraints = false
  68.         infoLabel.translatesAutoresizingMaskIntoConstraints = false
  69.        
  70.         addSubview(titleLabel)
  71.         addSubview(cView)
  72.         addSubview(infoLabel)
  73.  
  74.         NSLayoutConstraint.activate([
  75.            
  76.             titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 44.0),
  77.             titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
  78.             titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
  79.             titleLabel.heightAnchor.constraint(equalToConstant: 28.0),
  80.  
  81.             cView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8.0),
  82.             cView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0),
  83.             cView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0),
  84.             cView.heightAnchor.constraint(equalToConstant: 88.0),
  85.            
  86.             infoLabel.topAnchor.constraint(equalTo: cView.bottomAnchor, constant: 8.0),
  87.             infoLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
  88.             infoLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
  89.             infoLabel.heightAnchor.constraint(equalTo: titleLabel.heightAnchor),
  90.            
  91.         ])
  92.        
  93.         titleLabel.text = "Background View"
  94.         infoLabel.text = "Info"
  95.        
  96.         cView.dataSource = self
  97.         cView.delegate = self
  98.         cView.register(TVBkgCell.self, forCellWithReuseIdentifier: "c")
  99.     }
  100.    
  101.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  102.         return 30
  103.     }
  104.    
  105.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  106.         let c = collectionView.dequeueReusableCell(withReuseIdentifier: "c", for: indexPath) as! TVBkgCell
  107.         c.label.text = "\(indexPath.item)"
  108.         c.button.setTitle("Button \(indexPath.item)", for: [])
  109.         c.callBack = { [weak self] str in
  110.             guard let self = self else { return }
  111.             self.infoLabel.text = "Tapped: \(str)"
  112.         }
  113.         return c
  114.     }
  115.  
  116. }
  117.  
  118. class TVBkgCell: UICollectionViewCell {
  119.    
  120.     var callBack: ((String) -> ())?
  121.    
  122.     let label: UILabel = {
  123.         let v = UILabel()
  124.         v.backgroundColor = .yellow
  125.         v.textAlignment = .center
  126.         return v
  127.     }()
  128.    
  129.     let button: UIButton = {
  130.         let v = UIButton()
  131.         v.backgroundColor = .red
  132.         v.setTitleColor(.white, for: .normal)
  133.         v.setTitleColor(.lightGray, for: .highlighted)
  134.         return v
  135.     }()
  136.    
  137.     override init(frame: CGRect) {
  138.         super.init(frame: frame)
  139.         commonInit()
  140.     }
  141.     required init?(coder: NSCoder) {
  142.         super.init(coder: coder)
  143.         commonInit()
  144.     }
  145.     func commonInit() -> Void {
  146.         contentView.backgroundColor = .systemBlue
  147.        
  148.         label.translatesAutoresizingMaskIntoConstraints = false
  149.         button.translatesAutoresizingMaskIntoConstraints = false
  150.         contentView.addSubview(label)
  151.         contentView.addSubview(button)
  152.         let g = contentView.layoutMarginsGuide
  153.         NSLayoutConstraint.activate([
  154.            
  155.             label.topAnchor.constraint(equalTo: g.topAnchor),
  156.             label.leadingAnchor.constraint(equalTo: g.leadingAnchor),
  157.             label.trailingAnchor.constraint(equalTo: g.trailingAnchor),
  158.            
  159.             button.topAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor, constant: 8.0),
  160.             button.leadingAnchor.constraint(equalTo: g.leadingAnchor),
  161.             button.trailingAnchor.constraint(equalTo: g.trailingAnchor),
  162.             button.bottomAnchor.constraint(equalTo: g.bottomAnchor),
  163.            
  164.         ])
  165.        
  166.         button.addTarget(self, action: #selector(btnTap(_:)), for: .touchUpInside)
  167.     }
  168.    
  169.     @objc func btnTap(_ sender: UIButton) {
  170.         if let t = sender.currentTitle {
  171.             callBack?(t)
  172.         }
  173.     }
  174.    
  175. }
  176.  
  177.  
Add Comment
Please, Sign In to add comment