SBTContentViewCell.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // SBTContentViewCell.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/3/11.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class SBTContentViewCell: UITableViewCell {
  10. var cellCollectionView:UICollectionView?
  11. //回调方法
  12. var cellForItemAtIndexPathBlock: ((IndexPath)->String)?
  13. var numberOfItemsInSectionBlock: ((Int)->Int)?
  14. var sizeForItemAtIndexPathBlock: ((UICollectionViewLayout?,IndexPath)->CGSize)?
  15. var ContentViewCellDidScrollBlock: ((UIScrollView)->Void)?
  16. var cellWithColorAtIndexPathBlock: ((IndexPath)->Bool)?
  17. var didSelectItemBlock: ((IndexPath)->Void)?
  18. override func awakeFromNib() {
  19. super.awakeFromNib()
  20. // Initialization code
  21. }
  22. override func setSelected(_ selected: Bool, animated: Bool) {
  23. super.setSelected(selected, animated: animated)
  24. }
  25. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  26. super.init(style: style, reuseIdentifier: reuseIdentifier)
  27. let layout = UICollectionViewFlowLayout()
  28. layout.minimumLineSpacing = 1.0 //间隔
  29. layout.minimumInteritemSpacing = 0;
  30. layout.scrollDirection = .horizontal //滚动方向
  31. cellCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), collectionViewLayout: layout)
  32. cellCollectionView?.showsHorizontalScrollIndicator = false
  33. if #available(iOS 11.0, *) {
  34. cellCollectionView?.contentInsetAdjustmentBehavior = .always
  35. }
  36. //设置边框
  37. cellCollectionView?.backgroundColor = UIColor.red
  38. cellCollectionView?.layer.backgroundColor = UIColor.lightGray.cgColor
  39. cellCollectionView?.layer.borderWidth = 1
  40. cellCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "inner.cell")
  41. cellCollectionView?.delegate = self
  42. cellCollectionView?.dataSource = self
  43. contentView.addSubview(cellCollectionView!)
  44. }
  45. required init?(coder aDecoder: NSCoder) {
  46. fatalError("init(coder:) has not been implemented")
  47. }
  48. }
  49. extension SBTContentViewCell : UICollectionViewDelegate,UICollectionViewDataSource{
  50. //滚动代理
  51. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  52. if let contentViewCellDidScrollBlock = ContentViewCellDidScrollBlock {
  53. contentViewCellDidScrollBlock(scrollView)
  54. }
  55. }
  56. func numberOfSections(in collectionView: UICollectionView) -> Int {
  57. return 1
  58. }
  59. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  60. return numberOfItemsInSectionBlock!(section)
  61. }
  62. func collectionView(_ collectionView: UICollectionView,
  63. layout collectionViewLayout: UICollectionViewLayout,
  64. sizeForItemAt indexPath: IndexPath) -> CGSize {
  65. return sizeForItemAtIndexPathBlock!(collectionViewLayout,indexPath)
  66. }
  67. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  68. var intercell : UICollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "inner.cell", for: indexPath)
  69. if intercell != nil{
  70. for view in intercell!.contentView.subviews{
  71. view.removeFromSuperview()
  72. }
  73. }
  74. intercell!.layer.borderColor = UIColor.lightGray.cgColor
  75. intercell!.layer.borderWidth = 1.0
  76. let hasColor = false
  77. if hasColor == cellWithColorAtIndexPathBlock!(indexPath){
  78. intercell!.backgroundColor = hasColor ? UIColor.lightGray : UIColor.white
  79. let cellsize : CGSize = sizeForItemAtIndexPathBlock!(nil,indexPath)
  80. let width = cellsize.width
  81. let height = cellsize.height
  82. let rect = CGRect(x: 0, y: 0, width: width, height: height)
  83. let label = UILabel(frame: rect)
  84. label.text = cellForItemAtIndexPathBlock!(indexPath)
  85. label.textColor = UIColor.black
  86. label.textAlignment = .center
  87. intercell?.contentView.addSubview(label)
  88. }
  89. return intercell!
  90. }
  91. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  92. if let cellDidSelectBlock = didSelectItemBlock {
  93. cellDidSelectBlock(indexPath)
  94. }
  95. }
  96. }