ChangeMealCollectionView.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // ChangeMealCollectionView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/27.
  6. //
  7. import UIKit
  8. class ChangeMealCollectionView: UIView {
  9. @IBOutlet weak var collectView: UICollectionView!
  10. var changeFoodBlock : ((_ model : MealFoodMsgModel , _ index : Int)->Void)?
  11. var dataModel : SetMealDetailModel?
  12. override func awakeFromNib() {
  13. super.awakeFromNib()
  14. self.createCollectView()
  15. }
  16. //MARK: collectionView
  17. func createCollectView(){
  18. //设置布局
  19. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
  20. let itemSpac = 25.0
  21. layout.scrollDirection = .vertical //竖直
  22. layout.itemSize = CGSize.init(width: (kSCREEN_WIDTH - 81)/3, height: 155)
  23. //行距
  24. layout.minimumInteritemSpacing = 15
  25. layout.minimumLineSpacing = 15
  26. layout.sectionInset = .init(top: 25, left: itemSpac, bottom: 0, right: itemSpac)
  27. collectView.collectionViewLayout = layout
  28. collectView.backgroundColor = UIColor.clear
  29. // collectView.delegate = self
  30. // collectView.dataSource = self
  31. collectView.showsVerticalScrollIndicator = false
  32. collectView.isScrollEnabled = false
  33. collectView.register(withType: ChangeMealCollectItem.self)
  34. }
  35. func configModel(model : SetMealDetailModel){
  36. self.dataModel = model
  37. self.collectView.reloadData()
  38. }
  39. func replaceModle(model : MealFoodMsgModel , index : Int){
  40. if self.changeFoodBlock != nil{
  41. self.changeFoodBlock!(model,index)
  42. }
  43. }
  44. }
  45. //MARK: Delegate
  46. extension ChangeMealCollectionView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  47. {
  48. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  49. return self.dataModel?.mealFoodMsgs?.count ?? 0
  50. }
  51. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  52. let cell : ChangeMealCollectItem = collectionView.dequeueReusableCell(withReuseIdentifier: "ChangeMealCollectItem", for: indexPath) as! ChangeMealCollectItem
  53. cell.configDataSouce(model: self.dataModel!.mealFoodMsgs![indexPath.row])
  54. cell.changeFoodBlock = {[weak self] model in
  55. self?.replaceModle(model: model, index: indexPath.row)
  56. }
  57. return cell;
  58. }
  59. func numberOfSections(in collectionView: UICollectionView) -> Int {
  60. return 1
  61. }
  62. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  63. }
  64. }