// // MealCollectionView.swift // ADHTuanCan // // Created by 敖德亨 on 2023/10/26. // import UIKit class MealCollectionView: UIView { @IBOutlet weak var moreBtn: UIButton! @IBOutlet weak var morenBtnView: UIImageView! @IBOutlet weak var collectView: UICollectionView! //top 43 //bottom 50 var dataModel : SetMealDetailModel? /// 展示更多 var showMore = false var showMoreActionBlock : ((Bool)->Void)? var needKnowBlock : (()->Void)? var changeFoodBlock : (()->Void)? override func awakeFromNib() { super.awakeFromNib() self.createCollectView() } @IBAction func showMoreAction(_ sender: UIButton) { if self.dataModel?.mealFoodMsgs?.count ?? 0 < 9{ return } self.showMore = !self.showMore self.collectView.reloadData() if self.showMore{ self.moreBtn.setTitle(LanguagesUtil.createTextBy(Ctext: "收起", Etext: "pack up"), for: .normal) self.morenBtnView.image = UIImage.init(named: "上箭头") }else{ self.moreBtn.setTitle(LanguagesUtil.createTextBy(Ctext: "剩余商品", Etext: "more"), for: .normal) self.morenBtnView.image = UIImage.init(named: "下箭头") } if (self.showMoreActionBlock != nil) { self.showMoreActionBlock!(self.showMore) } } func configModel(model : SetMealDetailModel){ self.dataModel = model self.collectView.reloadData() } @IBAction func needKnowAction(_ sender: UIButton) { if self.needKnowBlock != nil{ self.needKnowBlock!() } } //MARK: collectionView func createCollectView(){ //设置布局 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init() let itemSpac = 25.0 layout.scrollDirection = .vertical //竖直 layout.itemSize = CGSize.init(width: (kSCREEN_WIDTH - 81)/3, height: 126) //行距 layout.minimumInteritemSpacing = 12 layout.minimumLineSpacing = 12 layout.sectionInset = .init(top: 12, left: itemSpac, bottom: 0, right: itemSpac) collectView.collectionViewLayout = layout collectView.backgroundColor = UIColor.clear collectView.delegate = self collectView.dataSource = self collectView.showsVerticalScrollIndicator = false collectView.isScrollEnabled = false collectView.register(withType: SetMealCollectionItem.self) } } //MARK: Delegate extension MealCollectionView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if (self.dataModel?.mealFoodMsgs?.count ?? 0) > 9{ if self.showMore{ return self.dataModel?.mealFoodMsgs?.count ?? 0 }else{ return (self.dataModel?.mealFoodMsgs?.count ?? 0) > 9 ? 9 : self.dataModel?.mealFoodMsgs?.count ?? 0 } } return self.dataModel?.mealFoodMsgs?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : SetMealCollectionItem = collectionView.dequeueReusableCell(withReuseIdentifier: "SetMealCollectionItem", for: indexPath) as! SetMealCollectionItem cell.configData(model: self.dataModel!.mealFoodMsgs![indexPath.row]) return cell; } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let model = self.dataModel!.mealFoodMsgs![indexPath.row] model.selected = !model.selected self.collectView.reloadData() if self.changeFoodBlock != nil{ self.changeFoodBlock!() } } }