// // ChangeMealCollectionView.swift // ADHTuanCan // // Created by 敖德亨 on 2023/10/27. // import UIKit class ChangeMealCollectionView: UIView { @IBOutlet weak var collectView: UICollectionView! var changeFoodBlock : ((_ model : MealFoodMsgModel , _ index : Int)->Void)? var dataModel : SetMealDetailModel? override func awakeFromNib() { super.awakeFromNib() self.createCollectView() } //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: 155) //行距 layout.minimumInteritemSpacing = 15 layout.minimumLineSpacing = 15 layout.sectionInset = .init(top: 25, 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: ChangeMealCollectItem.self) } func configModel(model : SetMealDetailModel){ self.dataModel = model self.collectView.reloadData() } func replaceModle(model : MealFoodMsgModel , index : Int){ if self.changeFoodBlock != nil{ self.changeFoodBlock!(model,index) } } } //MARK: Delegate extension ChangeMealCollectionView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataModel?.mealFoodMsgs?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : ChangeMealCollectItem = collectionView.dequeueReusableCell(withReuseIdentifier: "ChangeMealCollectItem", for: indexPath) as! ChangeMealCollectItem cell.configDataSouce(model: self.dataModel!.mealFoodMsgs![indexPath.row]) cell.changeFoodBlock = {[weak self] model in self?.replaceModle(model: model, index: indexPath.row) } return cell; } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { } }