IHRoomListView.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // IHRoomListView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/14.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHRoomListViewDelegate : NSObjectProtocol {
  10. func roomListView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
  11. func roomListViewOpration(_ devId:String,devType:String,status:String,currentIndex:IndexPath)
  12. }
  13. class IHRoomListView: UIView {
  14. var devDatas :[DevData]?{
  15. didSet{
  16. }
  17. }
  18. weak var delegate : IHRoomListViewDelegate?
  19. lazy var collectionView: UICollectionView = {
  20. let layout = UICollectionViewFlowLayout()
  21. let cellwidth = (KSCREENWIDTH - 22.5 - 22.5 - 20) / 2
  22. layout.itemSize = CGSize(width: cellwidth, height: cellwidth)//设置cell的大小
  23. layout.minimumLineSpacing = 20
  24. layout.minimumInteritemSpacing = 10 //同一列最小间隔
  25. layout.scrollDirection = .vertical //纵向滚动
  26. layout.sectionInset = UIEdgeInsets(top: 20, left: 22.5, bottom: 20, right: 22.5)//设置内边距
  27. let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH , height: KSCREENHEIGHT - KNavBarHeight - 50), collectionViewLayout: layout)
  28. collectionView.backgroundColor = UIColor.white
  29. collectionView.delegate = self
  30. collectionView.dataSource = self
  31. collectionView.showsHorizontalScrollIndicator = false
  32. collectionView.showsVerticalScrollIndicator = false
  33. collectionView.register(UINib(nibName: "IHIHRoomListCell", bundle: nil), forCellWithReuseIdentifier: "cell")
  34. return collectionView
  35. }()
  36. override init(frame: CGRect) {
  37. super.init(frame: frame)
  38. createUI()
  39. }
  40. func reloadData() {
  41. self.collectionView.reloadData()
  42. }
  43. func createUI() {
  44. addSubview(collectionView)
  45. }
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. }
  50. extension IHRoomListView:UICollectionViewDelegate,UICollectionViewDataSource{
  51. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  52. return devDatas?.count ?? 0
  53. }
  54. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  55. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? IHIHRoomListCell
  56. let model = self.devDatas![indexPath.row]
  57. cell?.model = model
  58. cell?.delegate = self
  59. cell!.currentIndexPath = indexPath
  60. return cell!
  61. }
  62. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  63. log.debug("点击了 \(indexPath.row)")
  64. if let delegate = delegate {
  65. delegate.roomListView(collectionView, didSelectItemAt: indexPath)
  66. }
  67. }
  68. }
  69. extension IHRoomListView :IHRoomListCellDelegate{
  70. func oprationSwitch(_ id: String, devType: String, status: String,currentIndex:IndexPath) {
  71. if let delegate = delegate {
  72. delegate.roomListViewOpration(id, devType: devType, status: status, currentIndex: currentIndex)
  73. }
  74. }
  75. }