优惠券swift版马甲包

YMTodayRecomCell.swift 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // YMTodayRecomCell.swift
  3. // MvpGoods
  4. //
  5. // Created by 小花 on 2019/4/11.
  6. // Copyright © 2019 MVP. All rights reserved.
  7. //
  8. import UIKit
  9. protocol YMTodayRecomCellDelegate: NSObjectProtocol {
  10. func didSelectedItem(index: Int,model: YMMainGoodModel)
  11. }
  12. class YMTodayRecomCell: UICollectionViewCell {
  13. weak var delegate: YMTodayRecomCellDelegate!
  14. var cellID = "cellId"
  15. var collection: UICollectionView?
  16. var dataArr: [YMMainGoodModel]?
  17. required init?(coder aDecoder: NSCoder) {
  18. self.init()
  19. }
  20. override init(frame: CGRect) {
  21. super.init(frame: frame)
  22. configSubViews()
  23. }
  24. func configSubViews() {
  25. let bg = UIImageView(frame: CGRect(x: 5, y: 5, width: SCREENW-10, height: fitSize(x: 129)))
  26. bg.backgroundColor = UIColor.gloadRedColor()
  27. bg.isUserInteractionEnabled = true
  28. bg.layer.cornerRadius = 5;
  29. self.addSubview(bg)
  30. let titleLb = UILabel(frame: CGRect(x: 19, y: fitSize(x: 7), width: 100, height: fitSize(x: 24)))
  31. titleLb.textColor = UIColor.white
  32. titleLb.font = UIFont.systemFont(ofSize: fitSize(x: 17))
  33. titleLb.text = "猜你喜欢"
  34. bg.addSubview(titleLb)
  35. let layout = UICollectionViewFlowLayout()
  36. layout.scrollDirection = .horizontal
  37. layout.itemSize = CGSize(width: fitSize(x: 118), height: fitSize(x: 187))
  38. layout.minimumLineSpacing = 10
  39. layout.minimumInteritemSpacing = 0
  40. let collectionView = UICollectionView(frame: CGRect(x: 0, y: fitSize(x: 38), width: self.width, height: fitSize(x: 190)), collectionViewLayout:layout)
  41. collectionView.delegate = self
  42. collectionView.dataSource = self
  43. collectionView.showsHorizontalScrollIndicator = false
  44. collectionView.backgroundColor = UIColor.clear
  45. collectionView.register(YMGroupChildCollectionCell.self, forCellWithReuseIdentifier: cellID)
  46. self.addSubview(collectionView)
  47. self.collection = collectionView
  48. }
  49. public func setDataArray(dataArr: [YMMainGoodModel]) {
  50. self.dataArr = dataArr
  51. self.collection?.reloadData()
  52. }
  53. }
  54. extension YMTodayRecomCell: UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
  55. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  56. return self.dataArr?.count ?? 0
  57. }
  58. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  59. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! YMGroupChildCollectionCell
  60. cell.model = self.dataArr?[indexPath.row]
  61. cell.imgView.layer.cornerRadius = fitSize(x: 118/2)
  62. cell.imgView.layer.masksToBounds = true
  63. cell.titleLabel.numberOfLines = 2
  64. cell.titleLabel.font = UIFont.systemFont(ofSize: 12)
  65. return cell
  66. }
  67. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  68. return UIEdgeInsetsMake(0, 10, 0, 20)
  69. }
  70. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  71. let model = self.dataArr?[indexPath.row]
  72. if (self.delegate != nil) {
  73. self.delegate?.didSelectedItem(index: indexPath.row, model: model!)
  74. }
  75. }
  76. }