优惠券swift版马甲包

YMOtherHeaderView.swift 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // YMOtherHeaderView.swift
  3. // MvpGoods
  4. //
  5. // Created by 小花 on 2019/4/11.
  6. // Copyright © 2019 MVP. All rights reserved.
  7. //
  8. import UIKit
  9. class YMOtherHeaderView: UIView {
  10. typealias ClickClosure = (_ model: YMOtherHeaderModel) -> Void
  11. var clickClosure: ClickClosure?
  12. var KMainGoodCell = "MainGoodCell"
  13. var KHeaderKey = "KHeaderKey"
  14. var collectionView: UICollectionView?
  15. var dataArr: [YMOtherHeaderModel]?
  16. override init(frame: CGRect) {
  17. super.init(frame: frame)
  18. self.initSubViews()
  19. }
  20. required init?(coder aDecoder: NSCoder) {
  21. fatalError("init(coder:) has not been implemented")
  22. }
  23. func initSubViews() {
  24. let layout = UICollectionViewFlowLayout()
  25. layout.scrollDirection = .horizontal
  26. layout.itemSize = CGSize(width: 70, height: 80)
  27. let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: self.height), collectionViewLayout:layout)
  28. collectionView.delegate = self
  29. collectionView.dataSource = self
  30. collectionView.showsHorizontalScrollIndicator = false
  31. collectionView.backgroundColor = UIColor.gloadGrayColor()
  32. collectionView.register(YMOtherHeaderCell.self, forCellWithReuseIdentifier: KMainGoodCell)
  33. collectionView.backgroundColor = UIColor.white
  34. self.addSubview(collectionView)
  35. self.collectionView = collectionView
  36. }
  37. func setDataArr(data: [YMOtherHeaderModel]){
  38. dataArr = data
  39. self.collectionView?.reloadData()
  40. }
  41. }
  42. extension YMOtherHeaderView: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
  43. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  44. return self.dataArr?.count ?? 0
  45. }
  46. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  47. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KMainGoodCell, for:indexPath) as! YMOtherHeaderCell
  48. cell.model = self.dataArr![indexPath.row]
  49. return cell
  50. }
  51. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  52. let model = self.dataArr?[indexPath.row]
  53. self.clickClosure?(model!)
  54. }
  55. }