SBTFormView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // SBTFormView.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/2/27.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class SheetLeftView: UITableView {
  10. override init(frame: CGRect, style: UITableView.Style) {
  11. super.init(frame: frame, style: style)
  12. }
  13. required init?(coder aDecoder: NSCoder) {
  14. fatalError("init(coder:) has not been implemented")
  15. }
  16. }
  17. class SheetBottomView: UICollectionView {
  18. override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
  19. super.init(frame: frame, collectionViewLayout: layout)
  20. }
  21. required init?(coder aDecoder: NSCoder) {
  22. fatalError("init(coder:) has not been implemented")
  23. }
  24. }
  25. class SheetContentView: UITableView {
  26. override init(frame: CGRect, style: UITableView.Style) {
  27. super.init(frame: frame, style: style)
  28. }
  29. required init?(coder aDecoder: NSCoder) {
  30. fatalError("init(coder:) has not been implemented")
  31. }
  32. }
  33. protocol SBTFormViewDataSource : NSObjectProtocol {
  34. //返回表格有多少行
  35. func formView(_ formView:SBTFormView,numberOfRowsIn section:Int ) -> Int
  36. //返回表格有多少列
  37. func formView(_ formView:SBTFormView,numberOfColsIn section:Int) -> Int
  38. //返回表格某行某列外支商需要显示的内容
  39. func formView(_ formView:SBTFormView,cellForContentItemAt indexRow:IndexPath, indexCol:IndexPath) -> String
  40. //返回表格左侧标题列每行需要显示的内容
  41. func formView(_ formView:SBTFormView,cellForLeftColAt indexPath:IndexPath) -> String
  42. //返回表格下边标题行每列需要显示的内容
  43. func formView(_ formView:SBTFormView,cellForBottomRowAt indexPath:IndexPath) -> String
  44. //返回表格某行是否需要填充颜色
  45. func formView(_ formView:SBTFormView,cellWithColorAt indexRow:IndexPath) -> Bool
  46. }
  47. protocol SBTFormViewDelegate : NSObjectProtocol {
  48. //返回表格每行的高度
  49. func formView(_ formView:SBTFormView,heightForRowAt indexPath:IndexPath ) -> CGFloat
  50. //返回表格每列的宽度
  51. func formView(_ formView:SBTFormView,widthForColAt indexPath:IndexPath) -> CGFloat
  52. //返回表格某行某列外支商需要显示的内容
  53. // func formView(_ formView:SBTFormView,cellForContentItemAt indexRow:IndexPath) -> String
  54. //点击事件
  55. func formView(_ formView:SBTFormView,didSelectItemAt indexRow:IndexPath, indexCol: IndexPath)
  56. }
  57. class SBTFormView: UIView {
  58. var weekData : [OneDayData]? //一周数据
  59. weak var dataSource : SBTFormViewDataSource?
  60. weak var delegate :SBTFormViewDelegate?
  61. let leftViewCellID = "left.tableview.cell"
  62. let bottomViewCellID = "top.collectionview.cell"
  63. let contentViewCellID = "content.tableview.cell"
  64. var sheetHead : String? // 第一行第一列格子j要显示的内容
  65. var titleColWidth : CGFloat? // 左侧标题列宽度 (必须设置)
  66. var titleRowHeight : CGFloat?// 下边标题行高度
  67. var leftView : SheetLeftView?
  68. var bottomView : SheetBottomView?
  69. var contentView : SheetContentView?
  70. var sheetHeadLabel : UILabel?
  71. override init(frame: CGRect) {
  72. super.init(frame: frame)
  73. createUI()
  74. }
  75. required init?(coder aDecoder: NSCoder) {
  76. fatalError("init(coder:) has not been implemented")
  77. }
  78. open func reloadData(){
  79. self.leftView?.reloadData()
  80. self.bottomView?.reloadData()
  81. self.contentView?.reloadData()
  82. }
  83. override func layoutSubviews() {
  84. super.layoutSubviews()
  85. let sheetViewWidth = self.frame.size.width
  86. let sheetViewHeight = self.frame.size.height
  87. self.leftView?.frame = CGRect(x: 0, y: 0, width: titleColWidth!, height: sheetViewHeight - titleRowHeight!)
  88. self.bottomView?.frame = CGRect(x: titleColWidth!, y: sheetViewHeight - titleRowHeight!, width: sheetViewWidth - titleColWidth!, height: titleRowHeight!)
  89. contentView?.frame = CGRect(x: titleColWidth!, y: 0, width: sheetViewWidth - titleColWidth!, height: sheetViewHeight - titleRowHeight!)
  90. }
  91. }
  92. extension SBTFormView{
  93. func createUI() {
  94. self.layer.borderColor = UIColor.red.cgColor
  95. self.layer.cornerRadius = 1
  96. self.layer.borderWidth = 1
  97. leftView = SheetLeftView(frame: CGRect.zero, style: .plain)
  98. leftView?.delegate = self
  99. leftView?.dataSource = self
  100. leftView?.showsVerticalScrollIndicator = false
  101. leftView?.backgroundColor = UIColor.clear
  102. leftView?.layer.borderColor = UIColor.blue.cgColor
  103. leftView?.layer.borderWidth = 1
  104. leftView?.separatorStyle = .none
  105. let layout = UICollectionViewFlowLayout()
  106. layout.minimumLineSpacing = 1
  107. layout.minimumInteritemSpacing = 1
  108. layout.scrollDirection = .horizontal
  109. bottomView = SheetBottomView(frame: CGRect.zero, collectionViewLayout: layout)
  110. bottomView?.delegate = self
  111. bottomView?.dataSource = self
  112. bottomView?.showsHorizontalScrollIndicator = false
  113. bottomView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: bottomViewCellID)
  114. bottomView?.backgroundColor = UIColor.white
  115. bottomView?.layer.borderWidth = 1
  116. bottomView?.layer.borderColor = UIColor.lightGray.cgColor
  117. contentView = SheetContentView(frame: CGRect.zero, style: .plain)
  118. contentView?.delegate = self
  119. contentView?.dataSource = self
  120. contentView?.showsVerticalScrollIndicator = false
  121. contentView?.separatorStyle = .none
  122. contentView?.backgroundColor = UIColor.white
  123. addSubview(leftView!)
  124. addSubview(bottomView!)
  125. addSubview(contentView!)
  126. }
  127. }
  128. extension SBTFormView:UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource{
  129. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  130. if scrollView.isKind(of: SheetLeftView.self) {
  131. contentView?.contentOffset = (leftView?.contentOffset)!
  132. }
  133. if scrollView.isKind(of: SheetLeftView.self) {
  134. leftView?.contentOffset = (contentView?.contentOffset)!
  135. }
  136. if self.isKind(of: UICollectionView.self) {
  137. for cell in (contentView?.visibleCells)! {
  138. let cell1 = cell as! SBTContentViewCell
  139. cell1.cellCollectionView?.contentOffset = scrollView.contentOffset
  140. }
  141. }
  142. }
  143. //MARK: -- UITableViewDelegate && UITableVIewDataSource
  144. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  145. return (delegate?.formView(self, heightForRowAt: indexPath))!
  146. }
  147. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  148. return (self.dataSource?.formView(self, numberOfRowsIn: section))!
  149. }
  150. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  151. if tableView.isKind(of: SheetLeftView.self) {
  152. var cell = tableView.dequeueReusableCell(withIdentifier: leftViewCellID)
  153. if cell == nil{
  154. cell = UITableViewCell(style: .default, reuseIdentifier: leftViewCellID)
  155. }
  156. cell?.selectionStyle = .none
  157. for view in (cell?.contentView.subviews)!{
  158. view.removeFromSuperview()
  159. }
  160. cell?.backgroundColor = UIColor.white
  161. cell?.layer.borderColor = UIColor.blue.cgColor
  162. cell?.layer.borderWidth = 1
  163. let width = self.titleColWidth
  164. let height = self.delegate?.formView(self, heightForRowAt: indexPath)
  165. let rect = CGRect(x: 0, y: 0, width: width!, height: height!)
  166. let label = UILabel(frame: rect)
  167. label.text = self.dataSource?.formView(self, cellForLeftColAt: indexPath)
  168. label.textColor = UIColor.black
  169. label.textAlignment = .center
  170. cell?.contentView.addSubview(label)
  171. return cell!
  172. }
  173. var aCell = tableView.dequeueReusableCell(withIdentifier: contentViewCellID) as? SBTContentViewCell
  174. if aCell == nil {
  175. aCell = SBTContentViewCell(style: .default, reuseIdentifier: contentViewCellID)
  176. }
  177. aCell?.selectionStyle = .none
  178. aCell?.cellForItemAtIndexPathBlock = { (indexPathInner) in
  179. // return (self.dataSource?.formView(self, cellForLeftColAt: indexPathInner))!
  180. return (self.dataSource?.formView(self, cellForContentItemAt: indexPath, indexCol: indexPathInner))!
  181. }
  182. aCell?.numberOfItemsInSectionBlock = {(section) in
  183. return (self.dataSource?.formView(self, numberOfRowsIn: section))!
  184. }
  185. aCell?.sizeForItemAtIndexPathBlock = {(collectionViewLayout,indexPathInner) in
  186. let width = self.delegate?.formView(self, widthForColAt: indexPathInner)
  187. let height = self.delegate?.formView(self, heightForRowAt: indexPathInner)
  188. return CGSize(width: width!, height: height!)
  189. }
  190. aCell?.ContentViewCellDidScrollBlock = {(scroll) in
  191. for cell in (self.contentView?.visibleCells)! {
  192. let cell2 = cell as! SBTContentViewCell
  193. cell2.cellCollectionView?.contentOffset = scroll.contentOffset
  194. }
  195. self.bottomView?.contentOffset = scroll.contentOffset
  196. }
  197. aCell?.cellWithColorAtIndexPathBlock = {(indexPathInner) in
  198. return (self.dataSource?.formView(self, cellWithColorAt: indexPath))!
  199. }
  200. aCell?.didSelectItemBlock = {(indexPathInner) in
  201. self.delegate?.formView(self, didSelectItemAt: indexPath, indexCol: indexPathInner)
  202. }
  203. aCell?.backgroundColor = UIColor.clear
  204. aCell?.cellCollectionView?.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
  205. aCell?.cellCollectionView?.reloadData()
  206. return aCell!
  207. }
  208. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  209. if tableView.isKind(of: SheetLeftView.self) {
  210. return
  211. }
  212. let willDisplayCell = cell as! SBTContentViewCell
  213. let didDisplayCell = tableView.visibleCells.first as! SBTContentViewCell
  214. if (willDisplayCell.cellCollectionView != nil) && (didDisplayCell.cellCollectionView != nil) && willDisplayCell.cellCollectionView?.contentOffset.x != didDisplayCell.cellCollectionView?.contentOffset.x {
  215. willDisplayCell.cellCollectionView?.contentOffset = (didDisplayCell.cellCollectionView?.contentOffset)!
  216. }
  217. }
  218. //MARK:pragma mark -- UICollectionViewDelegate && UICollectionViewDataSource
  219. func numberOfSections(in collectionView: UICollectionView) -> Int {
  220. return 1
  221. }
  222. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  223. return (self.dataSource?.formView(self, numberOfColsIn: section))!
  224. }
  225. func collectionView(_ collectionView: UICollectionView,
  226. layout collectionViewLayout: UICollectionViewLayout,
  227. sizeForItemAt indexPath: IndexPath) -> CGSize {
  228. let width = self.delegate?.formView(self, widthForColAt: indexPath)
  229. let height = self.titleRowHeight
  230. return CGSize(width: width!, height: height!)
  231. }
  232. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  233. let bottomCell = collectionView.dequeueReusableCell(withReuseIdentifier: bottomViewCellID, for: indexPath)
  234. if bottomCell != nil {
  235. for view in bottomCell.contentView.subviews{
  236. view.removeFromSuperview()
  237. }
  238. bottomCell.backgroundColor = UIColor.white
  239. bottomCell.layer.borderColor = UIColor.lightGray.cgColor
  240. bottomCell.layer.borderWidth = 1
  241. let width = self.delegate?.formView(self, widthForColAt: indexPath)
  242. let height = self.titleRowHeight
  243. let rect = CGRect(x: 0, y: 0, width: width!, height: height!)
  244. let label = UILabel(frame: rect)
  245. label.text = self.dataSource?.formView(self, cellForBottomRowAt: indexPath)
  246. label.textColor = UIColor.black
  247. label.textAlignment = .center
  248. bottomCell.contentView.addSubview(label)
  249. }
  250. return bottomCell
  251. }
  252. }