IHFloorListVCtr.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // IHFloorListVCtr.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/6.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftPopup
  10. import IBAnimatable
  11. class IHFloorListVCtr: AnimatableModalViewController {
  12. //默认选择
  13. var selected_def : Int? = 0
  14. var selectedCallback :((Int)->Void)?
  15. var floorList :[RoomListData]?{
  16. didSet{
  17. self.collectionView.reloadData()
  18. }
  19. }
  20. @IBOutlet weak var contentview: UIView!
  21. lazy var collectionView: UICollectionView = {
  22. let layout = UICollectionViewFlowLayout()
  23. layout.itemSize = CGSize(width: (UIScreen.main.bounds.width - 40) / 4, height: 45)//设置cell的大小
  24. layout.minimumLineSpacing = 0 //上下距离
  25. layout.minimumInteritemSpacing = 0 //左右距离
  26. layout.scrollDirection = .vertical
  27. let collectionView = UICollectionView(frame: CGRect(x: 20, y: 25, width: UIScreen.main.bounds.width - 40, height: KSCREENHEIGHT - 225), collectionViewLayout: layout)
  28. collectionView.isPagingEnabled = true // 允许分页
  29. collectionView.backgroundColor = UIColor.white
  30. collectionView.delegate = self
  31. collectionView.dataSource = self
  32. collectionView.showsHorizontalScrollIndicator = false
  33. collectionView.showsHorizontalScrollIndicator = false
  34. collectionView.register(UINib(nibName: "IHFloorListCell", bundle:nil), forCellWithReuseIdentifier: "cell")
  35. return collectionView
  36. }()
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. // self.showAnimation = nil
  40. // self.dismissAnimation = nil
  41. contentview.addSubview(collectionView)
  42. }
  43. @IBAction func cancelAction(_ sender: Any) {
  44. self.dismiss(animated: true)
  45. }
  46. }
  47. extension IHFloorListVCtr: UICollectionViewDelegate,UICollectionViewDataSource{
  48. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  49. return self.floorList?.count ?? 0
  50. }
  51. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  52. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! IHFloorListCell
  53. let model = self.floorList![indexPath.row]
  54. cell.floorName = model.name
  55. if indexPath.row == selected_def{
  56. cell.isSelected = true
  57. }
  58. return cell
  59. }
  60. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  61. cancellSelelcted()
  62. if let callback = self.selectedCallback {
  63. callback(indexPath.row)
  64. }
  65. self.dismiss(animated: true)
  66. }
  67. //取消所有选择
  68. private func cancellSelelcted() {
  69. let numSections = self.collectionView.numberOfSections
  70. for section in 0..<numSections {
  71. let numItems = self.collectionView.numberOfItems(inSection: section)
  72. for numItem in 0..<numItems {
  73. selectedCell(IndexPath(row: numItem, section: section))
  74. }
  75. }
  76. }
  77. private func selectedCell(_ indexPath :IndexPath) {
  78. if self.collectionView.cellForItem(at: indexPath) != nil {
  79. let cell = self.collectionView.cellForItem(at: indexPath)
  80. cell?.isSelected = false
  81. }
  82. }
  83. }