// // DeliveryTimeSetView.swift // ADHTuanCan // // Created by 敖德亨 on 2023/10/26. // import UIKit import RxSwift import RxCocoa class DeliveryTimeSetView: UIView { @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var numField: UITextField! override func awakeFromNib() { super.awakeFromNib() self.createCollectView() } var dataSource : [DeliveryTimeModel]? //MARK: RxSwift let disposeBag = DisposeBag() var selectTimeBlock : ((_ model : DeliveryTimeModel)->Void)? var inputNumBlock : ((String)->Void)? //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: 32) //行距 layout.minimumInteritemSpacing = 12 layout.minimumLineSpacing = 12 layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac) collectionView.collectionViewLayout = layout collectionView.backgroundColor = UIColor.clear collectionView.delegate = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false collectionView.isScrollEnabled = false collectionView.register(withType: DeliveryTimeItem.self) numField.inputFigure(maxLength: 4) { if (self.inputNumBlock != nil){ self.inputNumBlock!(self.numField.text ?? "") } } } func configModel(dataSource : [DeliveryTimeModel]?){ self.dataSource = dataSource self.collectionView.reloadData() } } //MARK: Delegate extension DeliveryTimeSetView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataSource?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : DeliveryTimeItem = collectionView.dequeueReusableCell(withReuseIdentifier: "DeliveryTimeItem", for: indexPath) as! DeliveryTimeItem cell.configModel(model: self.dataSource![indexPath.row]) return cell; } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let model = self.dataSource![indexPath.row] if model.status == "1"{ return } if model.quota == "0"{ return } for item in self.dataSource! { let data : DeliveryTimeModel = item data.selected = false } model.selected = true self.collectionView.reloadData() if (self.selectTimeBlock != nil){ self.selectTimeBlock!(model) } } }