CommunitySubCommentController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // CommunitySubCommentController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/12.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class CommunitySubCommentController: BaseViewController {
  10. var communityPostCommentModel : CommunityPostCommentModel?
  11. var communityPostReplyModels = Array<CommunityPostReplyModel>()
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. setupViews()
  15. setupLayouts()
  16. setupData()
  17. }
  18. override func viewWillAppear(_ animated: Bool) {
  19. cmtKeyboard.isDisappear = false
  20. }
  21. override func viewWillDisappear(_ animated: Bool) {
  22. cmtKeyboard.isDisappear = true
  23. }
  24. override func setupViews() {
  25. navigationBar.title = "3条评论"
  26. view.backgroundColor = kf7f8faColor
  27. view.addSubview(tableView)
  28. view.addSubview(cmtKeyboard)
  29. }
  30. override func setupLayouts() {
  31. tableView.snp.makeConstraints { (make) in
  32. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  33. make.left.right.bottom.equalTo(0)
  34. }
  35. }
  36. lazy var tableView: UITableView = {
  37. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  38. tableView.separatorStyle = .none
  39. tableView.backgroundColor = kf7f8faColor
  40. tableView.dataSource = self
  41. tableView.delegate = self
  42. tableView.estimatedRowHeight = 0
  43. tableView.estimatedSectionFooterHeight = 0
  44. tableView.estimatedSectionHeaderHeight = 0
  45. return tableView
  46. }()
  47. lazy var cmtKeyboard: LXKeyBoard = {
  48. let cmtKeyboard = LXKeyBoard.init()
  49. cmtKeyboard.backgroundColor = kffffffColor
  50. cmtKeyboard.maxLine = 3
  51. cmtKeyboard.font = kRegularFont14
  52. cmtKeyboard.topOrBottomEdge = 10
  53. cmtKeyboard.beginUpdateUI()
  54. cmtKeyboard.sendBlock = {
  55. [weak self] (text) in
  56. print("----\(text)")
  57. }
  58. return cmtKeyboard
  59. }()
  60. override func setupData() {
  61. tableView.addHeaderWithHeader(withBeginRefresh: true, animation: false) { [weak self] (page) in
  62. self?.communityPostReplyApi(page: page)
  63. }
  64. tableView.addFooterWithWithHeader(withAutomaticallyRefresh: false) {
  65. [weak self] (page) in
  66. self?.communityPostReplyApi(page: page)
  67. }
  68. }
  69. }
  70. extension CommunitySubCommentController {
  71. /// 获取子评论
  72. ///
  73. /// - Parameter page: 分页
  74. func communityPostReplyApi(page:Int) {
  75. SwiftMoyaNetWorkServiceCommunity.shared().communityPostReplyApi(postId: communityPostCommentModel?.id ?? 0, page: page) { [weak self] (communityPostReplysModel) -> (Void) in
  76. let communityPostReplysModel = communityPostReplysModel as? CommunityPostReplysModel
  77. if communityPostReplysModel?.pagination?.currentPage ?? 1 <= communityPostReplysModel?.pagination?.totalPages ?? 1 {
  78. if communityPostReplysModel?.pagination?.currentPage == 1{
  79. self?.communityPostReplyModels.removeAll()
  80. }
  81. self?.communityPostReplyModels = (self?.communityPostReplyModels)! + (communityPostReplysModel?.data!)!
  82. self?.tableView.reloadData()
  83. if self?.communityPostReplyModels.count ?? 0 >= communityPostReplysModel?.pagination?.total ?? 0 {
  84. self?.tableView.endFooterNoMoreData()
  85. }
  86. }else {
  87. self?.tableView.endFooterNoMoreData()
  88. }
  89. }
  90. }
  91. }
  92. // MARK: - tableView dataSource && delegate
  93. extension CommunitySubCommentController: UITableViewDataSource, UITableViewDelegate {
  94. func numberOfSections(in tableView: UITableView) -> Int {
  95. return 2
  96. }
  97. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  98. switch section {
  99. case 0:
  100. if communityPostCommentModel != nil {
  101. return 1
  102. }else {
  103. return 0
  104. }
  105. default:
  106. return communityPostReplyModels.isEmpty ? 0 : communityPostReplyModels.count
  107. }
  108. }
  109. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  110. switch indexPath.section {
  111. case 0:
  112. let cell = CommunityMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  113. cell.communityPostCommentModel = communityPostCommentModel
  114. return cell
  115. default:
  116. let cell = CommunityReplyCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  117. cell.communityPostReplyModel = communityPostReplyModels[indexPath.row]
  118. return cell
  119. }
  120. }
  121. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  122. }
  123. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  124. switch indexPath.section {
  125. case 0:
  126. return communityPostCommentModel?.height ?? 0
  127. default:
  128. return communityPostReplyModels[indexPath.row].height ?? 0
  129. }
  130. }
  131. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  132. return 10
  133. }
  134. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  135. return UIView()
  136. }
  137. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  138. return 0.000001
  139. }
  140. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  141. return UIView()
  142. }
  143. }