RecommendSimilarCell.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // RecommendSimilarCell.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/14.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 推荐图文内容--相关推荐のCell
  8. import UIKit
  9. class RecommendSimilarCell: UITableViewCell {
  10. class func cellWith(tableView:UITableView,indexPath:IndexPath) -> RecommendSimilarCell {
  11. let ID = "RecommendSimilarCell"
  12. tableView.register(RecommendSimilarCell.self, forCellReuseIdentifier: ID)
  13. let cell : RecommendSimilarCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! RecommendSimilarCell
  14. cell.indexPath = indexPath
  15. return cell
  16. }
  17. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  18. super.init(style: style, reuseIdentifier: reuseIdentifier)
  19. setupViews()
  20. setupLayouts()
  21. }
  22. required init?(coder aDecoder: NSCoder) {
  23. fatalError("init(coder:) has not been implemented")
  24. }
  25. var indexPath: IndexPath? {
  26. didSet {
  27. }
  28. }
  29. //MRAK: - 设置View
  30. private func setupViews() {
  31. self.selectionStyle = .none
  32. backgroundColor = kf7f8faColor
  33. addSubview(collectionView)
  34. }
  35. private func setupLayouts() {
  36. collectionView.snp.makeConstraints { (make) in
  37. make.top.equalTo(0)
  38. make.left.equalTo(5)
  39. make.right.equalTo(-5)
  40. make.height.equalTo(200)
  41. }
  42. }
  43. private lazy var collectionView: UICollectionView = {
  44. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  45. collectionView.backgroundColor = kf7f8faColor
  46. collectionView.delegate = self;
  47. collectionView.dataSource = self;
  48. collectionView.showsVerticalScrollIndicator = false
  49. collectionView.showsHorizontalScrollIndicator = false
  50. collectionView.bounces = false
  51. collectionView.isScrollEnabled = false
  52. return collectionView
  53. }()
  54. private lazy var collectionViewLayout: WaterFallLayout = {
  55. let collectionViewLayout = WaterFallLayout()
  56. collectionViewLayout.delegate = self
  57. collectionViewLayout.scrollDirection = .horizontal
  58. return collectionViewLayout
  59. }()
  60. var collectionViewHeight : CGFloat?
  61. //加载数据
  62. func reloadData() {
  63. //collectionView重新加载数据
  64. self.collectionView.reloadData()
  65. //更新collectionView的高度约束
  66. let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
  67. collectionViewHeight = contentSize.height
  68. self.collectionView.snp.remakeConstraints { (make) in
  69. make.top.equalTo(0)
  70. make.left.equalTo(5)
  71. make.right.equalTo(-5)
  72. make.height.equalTo(contentSize.height)
  73. make.bottom.lessThanOrEqualToSuperview()
  74. }
  75. self.collectionView.collectionViewLayout.invalidateLayout()
  76. }
  77. var heights : Array<CGFloat>?
  78. var communityPostDataModels : Array<CommunityPostDataModel>? {
  79. didSet {
  80. self.collectionView.reloadData()
  81. }
  82. }
  83. var heightModel : HeightModel?{
  84. didSet {
  85. heightModel?.height = collectionViewHeight!
  86. }
  87. }
  88. }
  89. extension RecommendSimilarCell : WaterFallLayoutDelegate {
  90. func collectionView(_ collectionView: UICollectionView!, heightOfItemAt indexPath: IndexPath!) -> CGFloat {
  91. return heights?.isEmpty ?? true ? 0 : heights?[indexPath.row] ?? 0
  92. }
  93. func contentInsets(for collectionView: UICollectionView!) -> UIEdgeInsets {
  94. return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
  95. }
  96. func columenCounts(for collectionView: UICollectionView!) -> Int32 {
  97. return 2
  98. }
  99. func horizontalGap(for collectionView: UICollectionView!) -> CGFloat {
  100. return 5
  101. }
  102. func verticalGap(for collectionView: UICollectionView!) -> CGFloat {
  103. return 5
  104. }
  105. }
  106. extension RecommendSimilarCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  107. func numberOfSections(in collectionView: UICollectionView) -> Int {
  108. return 1
  109. }
  110. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  111. return communityPostDataModels?.isEmpty ?? true ? 0 : communityPostDataModels?.count ?? 0
  112. }
  113. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  114. let cell = SearchContentListCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  115. cell.communityPostDataModel = communityPostDataModels?[indexPath.row]
  116. cell.userClosure = {
  117. [weak self] in
  118. if cell.communityPostDataModel?.uid != UserModel.shared().getModel()?.uid {
  119. let vc = OtherPersonalCenterViewController()
  120. vc.uid = cell.communityPostDataModel?.uid ?? 0
  121. self?.findViewController().navigationController?.pushViewController(vc, animated: true)
  122. }
  123. }
  124. return cell
  125. }
  126. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  127. let communityPostDataModel = communityPostDataModels?[indexPath.row]
  128. if PostType(rawValue: communityPostDataModel?.type ?? "image") == .image || PostType(rawValue: communityPostDataModel?.type ?? "html") == .html {
  129. let vc = CommunityRecommendController()
  130. vc.id = communityPostDataModel?.id ?? 0
  131. findViewController().navigationController?.pushViewController(vc, animated: true)
  132. }else {
  133. let vc = CommunityVideoListController()
  134. vc.contentId = communityPostDataModel?.id ?? 0
  135. findViewController().navigationController?.pushViewController(vc, animated: true)
  136. }
  137. }
  138. }