DeliveryTimeSetView.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // DeliveryTimeSetView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/26.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. class DeliveryTimeSetView: UIView {
  11. @IBOutlet weak var collectionView: UICollectionView!
  12. @IBOutlet weak var numField: UITextField!
  13. override func awakeFromNib() {
  14. super.awakeFromNib()
  15. self.createCollectView()
  16. }
  17. var dataSource : [DeliveryTimeModel]?
  18. //MARK: RxSwift
  19. let disposeBag = DisposeBag()
  20. var selectTimeBlock : ((_ model : DeliveryTimeModel)->Void)?
  21. var inputNumBlock : ((String)->Void)?
  22. //MARK: collectionView
  23. func createCollectView(){
  24. //设置布局
  25. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
  26. let itemSpac = 25.0
  27. layout.scrollDirection = .vertical //竖直
  28. layout.itemSize = CGSize.init(width: (kSCREEN_WIDTH - 81)/3, height: 32)
  29. //行距
  30. layout.minimumInteritemSpacing = 12
  31. layout.minimumLineSpacing = 12
  32. layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac)
  33. collectionView.collectionViewLayout = layout
  34. collectionView.backgroundColor = UIColor.clear
  35. collectionView.delegate = self
  36. collectionView.dataSource = self
  37. collectionView.showsVerticalScrollIndicator = false
  38. collectionView.isScrollEnabled = false
  39. collectionView.register(withType: DeliveryTimeItem.self)
  40. numField.inputNumber(maxLength: 4) {
  41. if (self.inputNumBlock != nil){
  42. self.inputNumBlock!(self.numField.text ?? "")
  43. }
  44. }
  45. }
  46. func configModel(dataSource : [DeliveryTimeModel]?){
  47. self.dataSource = dataSource
  48. self.collectionView.reloadData()
  49. }
  50. }
  51. //MARK: Delegate
  52. extension DeliveryTimeSetView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  53. {
  54. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  55. return self.dataSource?.count ?? 0
  56. }
  57. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  58. let cell : DeliveryTimeItem = collectionView.dequeueReusableCell(withReuseIdentifier: "DeliveryTimeItem", for: indexPath) as! DeliveryTimeItem
  59. cell.configModel(model: self.dataSource![indexPath.row])
  60. return cell;
  61. }
  62. func numberOfSections(in collectionView: UICollectionView) -> Int {
  63. return 1
  64. }
  65. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  66. let model = self.dataSource![indexPath.row]
  67. if model.status == "1"{
  68. return
  69. }
  70. if model.quota == "0"{
  71. return
  72. }
  73. for item in self.dataSource! {
  74. let data : DeliveryTimeModel = item
  75. data.selected = false
  76. }
  77. model.selected = true
  78. self.collectionView.reloadData()
  79. if (self.selectTimeBlock != nil){
  80. self.selectTimeBlock!(model)
  81. }
  82. }
  83. }