MealCollectionView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // MealCollectionView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/26.
  6. //
  7. import UIKit
  8. class MealCollectionView: UIView {
  9. @IBOutlet weak var moreBtn: UIButton!
  10. @IBOutlet weak var morenBtnView: UIImageView!
  11. @IBOutlet weak var collectView: UICollectionView!
  12. //top 43
  13. //bottom 50
  14. var dataModel : SetMealDetailModel?
  15. /// 展示更多
  16. var showMore = false
  17. var showMoreActionBlock : ((Bool)->Void)?
  18. var needKnowBlock : (()->Void)?
  19. var changeFoodBlock : (()->Void)?
  20. override func awakeFromNib() {
  21. super.awakeFromNib()
  22. self.createCollectView()
  23. }
  24. @IBAction func showMoreAction(_ sender: UIButton) {
  25. if self.dataModel?.mealFoodMsgs?.count ?? 0 < 9{
  26. return
  27. }
  28. self.showMore = !self.showMore
  29. self.collectView.reloadData()
  30. if self.showMore{
  31. self.moreBtn.setTitle(LanguagesUtil.createTextBy(Ctext: "收起", Etext: "pack up"), for: .normal)
  32. self.morenBtnView.image = UIImage.init(named: "上箭头")
  33. }else{
  34. self.moreBtn.setTitle(LanguagesUtil.createTextBy(Ctext: "剩余商品", Etext: "more"), for: .normal)
  35. self.morenBtnView.image = UIImage.init(named: "下箭头")
  36. }
  37. if (self.showMoreActionBlock != nil)
  38. {
  39. self.showMoreActionBlock!(self.showMore)
  40. }
  41. }
  42. func configModel(model : SetMealDetailModel){
  43. self.dataModel = model
  44. self.collectView.reloadData()
  45. }
  46. @IBAction func needKnowAction(_ sender: UIButton) {
  47. if self.needKnowBlock != nil{
  48. self.needKnowBlock!()
  49. }
  50. }
  51. //MARK: collectionView
  52. func createCollectView(){
  53. //设置布局
  54. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
  55. let itemSpac = 25.0
  56. layout.scrollDirection = .vertical //竖直
  57. layout.itemSize = CGSize.init(width: (kSCREEN_WIDTH - 81)/3, height: 126)
  58. //行距
  59. layout.minimumInteritemSpacing = 12
  60. layout.minimumLineSpacing = 12
  61. layout.sectionInset = .init(top: 12, left: itemSpac, bottom: 0, right: itemSpac)
  62. collectView.collectionViewLayout = layout
  63. collectView.backgroundColor = UIColor.clear
  64. collectView.delegate = self
  65. collectView.dataSource = self
  66. collectView.showsVerticalScrollIndicator = false
  67. collectView.isScrollEnabled = false
  68. collectView.register(withType: SetMealCollectionItem.self)
  69. }
  70. }
  71. //MARK: Delegate
  72. extension MealCollectionView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  73. {
  74. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  75. if (self.dataModel?.mealFoodMsgs?.count ?? 0) > 9{
  76. if self.showMore{
  77. return self.dataModel?.mealFoodMsgs?.count ?? 0
  78. }else{
  79. return (self.dataModel?.mealFoodMsgs?.count ?? 0) > 9 ? 9 : self.dataModel?.mealFoodMsgs?.count ?? 0
  80. }
  81. }
  82. return self.dataModel?.mealFoodMsgs?.count ?? 0
  83. }
  84. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  85. let cell : SetMealCollectionItem = collectionView.dequeueReusableCell(withReuseIdentifier: "SetMealCollectionItem", for: indexPath) as! SetMealCollectionItem
  86. cell.configData(model: self.dataModel!.mealFoodMsgs![indexPath.row])
  87. return cell;
  88. }
  89. func numberOfSections(in collectionView: UICollectionView) -> Int {
  90. return 1
  91. }
  92. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  93. let model = self.dataModel!.mealFoodMsgs![indexPath.row]
  94. model.selected = !model.selected
  95. self.collectView.reloadData()
  96. if self.changeFoodBlock != nil{
  97. self.changeFoodBlock!()
  98. }
  99. }
  100. }