CommunityFeaturedTopicsViewController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. //
  2. // CommunityFeaturedTopicsViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/6/16.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import Kingfisher
  10. import SwiftyMediator
  11. class CommunityFeaturedTopicsViewController: BaseViewController {
  12. override func didReceiveMemoryWarning() {
  13. super.didReceiveMemoryWarning()
  14. KingfisherManager.shared.cache.clearDiskCache()
  15. KingfisherManager.shared.cache.clearMemoryCache()
  16. }
  17. deinit {
  18. NXLLog("deinit")
  19. if observe != nil {
  20. NotificationCenter.default.removeObserver(observe!)
  21. }
  22. }
  23. weak var observe : NSObjectProtocol?
  24. var id : Int = 0
  25. var communityRecommendDataModels = Array<CommunityRecommendDataModel>()
  26. var communityTopicDetailModel : CommunityTopicDetailModel?
  27. /// 评论需要的模型
  28. var communityRecommendDataModel : CommunityRecommendDataModel?
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. setupViews()
  32. setupData()
  33. setupLayouts()
  34. }
  35. override func viewWillAppear(_ animated: Bool) {
  36. super.viewWillAppear(animated)
  37. communityTopicDetailApi()
  38. }
  39. override func setupViews() {
  40. view.backgroundColor = UIColor.white
  41. view.addSubview(bgImageView)
  42. view.addSubview(bgView)
  43. view.addSubview(tableView)
  44. view.insertSubview(navigationBar, aboveSubview: tableView)
  45. setnavigationBar()
  46. }
  47. /// 设置navigationBar
  48. func setnavigationBar() {
  49. navigationBar.addSubview(navigationBgView)
  50. navigationBar.addSubview(topicLable)
  51. navigationBar.addSubview(focusButton)
  52. navigationBar.insertSubview(navigationBgView, aboveSubview: navigationBar.backgroundImageView)
  53. navigationBar.backgroundImageView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth)
  54. navigationBar.clipsToBounds = true
  55. navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_white")!)
  56. navigationBar.wr_setRightButton(title: "我的话题", titleColor: .white)
  57. // 设置初始导航栏透明度
  58. navigationBar.wr_setBackgroundAlpha(alpha: 0)
  59. // 设置状态栏style
  60. statusBarStyle = .lightContent
  61. }
  62. override func setupLayouts() {
  63. bgImageView.snp_makeConstraints { (make) in
  64. make.top.left.right.equalToSuperview()
  65. make.height.equalTo(kScreenWidth)
  66. }
  67. bgView.snp.makeConstraints { (make) in
  68. make.top.left.right.equalToSuperview()
  69. make.height.equalTo(kScreenWidth)
  70. }
  71. tableView.snp.makeConstraints { (make) in
  72. make.top.left.right.bottom.equalToSuperview()
  73. }
  74. navigationBgView.snp_makeConstraints { (make) in
  75. make.edges.equalTo(navigationBar)
  76. }
  77. topicLable.snp_makeConstraints { (make) in
  78. make.centerY.equalTo(navigationBar.leftButton)
  79. make.left.equalTo(navigationBar.leftButton.snp_right)
  80. }
  81. focusButton.snp_makeConstraints { (make) in
  82. make.right.equalTo(-14)
  83. make.centerY.equalTo(navigationBar.leftButton)
  84. make.width.equalTo(60)
  85. make.height.equalTo(28)
  86. }
  87. }
  88. private lazy var tableView: UITableView = {
  89. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  90. tableView.separatorStyle = .none
  91. tableView.backgroundColor = UIColor.clear
  92. tableView.dataSource = self
  93. tableView.delegate = self
  94. if #available(iOS 11.0, *) {
  95. tableView.contentInsetAdjustmentBehavior = .never
  96. }
  97. return tableView
  98. }()
  99. private lazy var bgImageView: UIImageView = {
  100. let bgImageView = UIImageView()
  101. bgImageView.contentMode = .scaleAspectFill
  102. bgImageView.clipsToBounds = true
  103. return bgImageView
  104. }()
  105. private lazy var bgView: UIView = {
  106. let bgView = UIView()
  107. bgView.backgroundColor = UIColor(hexString: "000000", alpha: 0.4)
  108. return bgView
  109. }()
  110. private lazy var communityFeaturedTopicsHeaderView: CommunityFeaturedTopicsHeaderView = {
  111. let communityFeaturedTopicsHeaderView = CommunityFeaturedTopicsHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 180 + kSafeStatusBarHeight))
  112. return communityFeaturedTopicsHeaderView
  113. }()
  114. private lazy var topicLable: UILabel = {
  115. let topicLable = UILabel()
  116. topicLable.textColor = kffffffColor
  117. topicLable.font = kRegularFont14
  118. topicLable.text = "运动健身"
  119. topicLable.alpha = 0
  120. return topicLable
  121. }()
  122. private lazy var focusButton: UIButton = {
  123. let focusButton = UIButton(type: UIButton.ButtonType.custom)
  124. focusButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  125. focusButton.setTitleColor(kffffffColor, for: UIControl.State.selected)
  126. focusButton.setTitle("关注", for: UIControl.State.normal)
  127. focusButton.setTitle("已关注", for: UIControl.State.selected)
  128. focusButton.titleLabel?.font = kRegularFont13
  129. focusButton.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
  130. focusButton.setBackgroundImage(UIImage.imageWithColor(color: UIColor.clear), for: UIControl.State.selected)
  131. focusButton.alpha = 0
  132. focusButton.layer.borderWidth = 0.5
  133. focusButton.layer.borderColor = kThemeColor.cgColor
  134. focusButton.cornerRadius = 14
  135. focusButton.masksToBounds = true
  136. focusButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  137. if self?.communityTopicDetailModel?.isFollow == 1 {
  138. CommunityFollowTopicViewModel.shared.communityMemberFollowTopicCancelFollowApi(topicId: (self?.id)!, communityTopicDetailModel: (self?.communityTopicDetailModel!)!, communityFeaturedTopicsHeaderView: (self?.communityFeaturedTopicsHeaderView)!, button: (self?.focusButton)!, communityFollowTopicType: .view)
  139. }else {
  140. CommunityFollowTopicViewModel.shared.communityMemberFollowTopicFollowApi(topicId: (self?.id)!, communityTopicDetailModel: (self?.communityTopicDetailModel!)!, communityFeaturedTopicsHeaderView: (self?.communityFeaturedTopicsHeaderView)!, button: (self?.focusButton)!, communityFollowTopicType: .view)
  141. }
  142. }).disposed(by: disposeBag)
  143. return focusButton
  144. }()
  145. private lazy var navigationBgView: UIView = {
  146. let navigationBgView = UIView()
  147. navigationBgView.backgroundColor = UIColor(hexString: "000000", alpha: 0.4)
  148. navigationBgView.alpha = 0
  149. return navigationBgView
  150. }()
  151. override func setupData() {
  152. //下拉刷新
  153. tableView.addFullScreenHeader(withBeginRefresh: true, animation: true) {
  154. [weak self] (page) in
  155. self?.communityTopicDetailApi()
  156. self?.communityPostTopicApi(page: page)
  157. }
  158. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  159. [weak self] (page) in
  160. self?.communityPostTopicApi(page: page)
  161. })
  162. communityFeaturedTopicsHeaderView.followClosureName = {
  163. [weak self] in
  164. if self?.communityTopicDetailModel?.isFollow == 1 {
  165. CommunityFollowTopicViewModel.shared.communityMemberFollowTopicCancelFollowApi(topicId: (self?.id)!, communityTopicDetailModel: (self?.communityTopicDetailModel!)!, communityFeaturedTopicsHeaderView: (self?.communityFeaturedTopicsHeaderView)!, button: (self?.focusButton)!, communityFollowTopicType: .view)
  166. }else {
  167. CommunityFollowTopicViewModel.shared.communityMemberFollowTopicFollowApi(topicId: (self?.id)!, communityTopicDetailModel: (self?.communityTopicDetailModel!)!, communityFeaturedTopicsHeaderView: (self?.communityFeaturedTopicsHeaderView)!, button: (self?.focusButton)!, communityFollowTopicType: .view)
  168. }
  169. }
  170. navigationBar.onClickRightButton = {
  171. [weak self] in
  172. let vc = CommunityMyFollowTopicController()
  173. self?.navigationController?.pushViewController(vc, animated: true)
  174. }
  175. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  176. let followStatusModel = notification.object as? FollowStatusModel
  177. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  178. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  179. if communityRecommendDataModel.uid == followStatusModel?.uid {
  180. communityRecommendDataModel.isFollow = followStatusModel?.isFollowStatus
  181. }
  182. }
  183. }
  184. self?.tableView.reloadData()
  185. }
  186. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  187. let followStatusModel = notification.object as? FollowStatusModel
  188. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  189. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  190. if communityRecommendDataModel.id == followStatusModel?.postId {
  191. communityRecommendDataModel.isDislike = followStatusModel?.isFollowStatus
  192. }
  193. }
  194. }
  195. self?.tableView.reloadData()
  196. }
  197. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  198. let followStatusModel = notification.object as? FollowStatusModel
  199. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  200. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  201. if communityRecommendDataModel.id == followStatusModel?.postId {
  202. communityRecommendDataModel.isLike = followStatusModel?.isFollowStatus
  203. }
  204. }
  205. }
  206. self?.tableView.reloadData()
  207. }
  208. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  209. let followStatusModel = notification.object as? FollowStatusModel
  210. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  211. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  212. if communityRecommendDataModel.id == followStatusModel?.postId {
  213. communityRecommendDataModel.isCollect = followStatusModel?.isFollowStatus
  214. }
  215. }
  216. }
  217. self?.tableView.reloadData()
  218. }
  219. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCommnetLikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  220. let followStatusModel = notification.object as? FollowStatusModel
  221. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  222. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  223. if communityRecommendDataModel.id == followStatusModel?.postId {
  224. if !(communityRecommendDataModel.comment?.isEmpty ?? true ){
  225. for communityRecommendCommentModel in communityRecommendDataModel.comment! {
  226. if communityRecommendCommentModel.id == followStatusModel?.commnetId {
  227. communityRecommendCommentModel.isLike = followStatusModel?.isFollowStatus
  228. communityRecommendCommentModel.likeCount = followStatusModel?.commentLikeCount
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. self?.tableView.reloadData()
  236. }
  237. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("communityDeletePostApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  238. let postId = notification.object as? Int
  239. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  240. for (index,communityRecommendDataModel) in (self?.communityRecommendDataModels)!.enumerated() {
  241. if communityRecommendDataModel.id == postId {
  242. self?.communityRecommendDataModels.remove(at: index)
  243. }
  244. }
  245. self?.tableView.reloadData()
  246. }
  247. }
  248. }
  249. /// 显示键盘
  250. func showKeyBoardCommentView() {
  251. KeyBoardInputView.show(inputViewResultClosure: {
  252. [weak self] (text) in
  253. self?.communityPostCommentApi(text: text, complete: {
  254. [weak self] in
  255. self?.communityRecommendDataModel = nil
  256. })
  257. }) {
  258. [weak self] in
  259. self?.communityRecommendDataModel = nil
  260. }
  261. }
  262. }
  263. extension CommunityFeaturedTopicsViewController {
  264. func scrollViewDidScroll(_ scrollView: UIScrollView)
  265. {
  266. let offsetY = scrollView.contentOffset.y
  267. if (offsetY > kSafeStatusBarHeight)
  268. {
  269. let alpha = (offsetY - kSafeStatusBarHeight) / CGFloat(kNavBarTotalHeight)
  270. navigationBar.wr_setBackgroundAlpha(alpha: alpha)
  271. focusButton.alpha = alpha
  272. topicLable.alpha = alpha
  273. navigationBgView.alpha = alpha
  274. navigationBar.rightButton.alpha = 1 - alpha
  275. }
  276. else
  277. {
  278. navigationBar.wr_setBackgroundAlpha(alpha: 0)
  279. focusButton.alpha = 0
  280. topicLable.alpha = 0
  281. navigationBgView.alpha = 0
  282. navigationBar.rightButton.alpha = 1
  283. }
  284. }
  285. }
  286. extension CommunityFeaturedTopicsViewController {
  287. /// 话题详情页面
  288. func communityTopicDetailApi() {
  289. SwiftMoyaNetWorkServiceCommunity.shared().communityTopicDetailApi(id: id) {
  290. [weak self] (communityTopicDetailModel) -> (Void) in
  291. self?.communityTopicDetailModel = communityTopicDetailModel as? CommunityTopicDetailModel
  292. self?.topicLable.text = self?.communityTopicDetailModel?.name
  293. CommunityFollowTopicViewModel.shared.followTopicButton(communityTopicDetailModel: self?.communityTopicDetailModel, button: self?.focusButton)
  294. self?.bgImageView.kf.setImage(with: kURLImage(name: self?.communityTopicDetailModel?.img ?? ""), placeholder: kImage(name: "default_pic"))
  295. self?.navigationBar.barBackgroundImage = self?.bgImageView.image
  296. self?.communityFeaturedTopicsHeaderView.communityTopicDetailModel = self?.communityTopicDetailModel
  297. self?.tableView.tableHeaderView = self?.communityFeaturedTopicsHeaderView
  298. }
  299. }
  300. /// 话题帖子
  301. func communityPostTopicApi(page:Int) {
  302. SwiftMoyaNetWorkServiceCommunity.shared().communityPostTopicApi(id: id, page: page, completion: {
  303. [weak self] (communityRecommendFeedModel) -> (Void) in
  304. let communityRecommendFeedModel = communityRecommendFeedModel as? CommunityRecommendFeedModel
  305. if communityRecommendFeedModel?.pagination?.currentPage == 1{
  306. self?.communityRecommendDataModels.removeAll()
  307. self?.tableView.resetNoMoreData()
  308. }
  309. self?.communityRecommendDataModels = (self?.communityRecommendDataModels)! + (communityRecommendFeedModel?.data!)!
  310. self?.tableView.reloadData()
  311. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityRecommendFeedModel?.pagination)
  312. }) {
  313. [weak self] loadingStatus in
  314. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  315. }
  316. }
  317. /// 评论
  318. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  319. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  320. communityCustomCommnetModel.postId = communityRecommendDataModel?.id ?? 0
  321. communityCustomCommnetModel.content = text
  322. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  323. [weak self] (communityPostCommentIdModel) -> (Void) in
  324. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  325. let communityRecommendCommentModel = CommunityRecommendCommentModel()
  326. communityRecommendCommentModel.content = text
  327. communityRecommendCommentModel.id = communityPostCommentIdModel?.id
  328. communityRecommendCommentModel.username = UserModel.shared().getModel()?.username
  329. communityRecommendCommentModel.uid = UserModel.shared().getModel()?.uid
  330. if self?.communityRecommendDataModel?.comment?.isEmpty ?? true {
  331. self?.communityRecommendDataModel?.comment = Array<CommunityRecommendCommentModel>()
  332. }
  333. self?.communityRecommendDataModel?.comment?.insert(communityRecommendCommentModel, at: 0)
  334. VirusViewModel.shared.comment(communityRecommendDataModel: self?.communityRecommendDataModel, id: communityPostCommentIdModel?.id ?? 0,content: text)
  335. let count = self?.communityRecommendDataModel?.commentCount ?? 0 + 1
  336. self?.communityRecommendDataModel?.commentCount = count
  337. self?.tableView.reloadData()
  338. complete()
  339. }
  340. }
  341. /// 删除帖子
  342. func communityDeleteApi(postId:Int,section:Int) {
  343. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  344. self?.communityRecommendDataModels.remove(at: section)
  345. self?.tableView.reloadData()
  346. }
  347. }
  348. }
  349. extension CommunityFeaturedTopicsViewController : UITableViewDelegate,UITableViewDataSource {
  350. func numberOfSections(in tableView: UITableView) -> Int {
  351. return communityRecommendDataModels.isEmpty ? 0 : communityRecommendDataModels.count
  352. }
  353. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  354. return 6
  355. }
  356. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  357. let communityRecommendDataModel = communityRecommendDataModels[indexPath.section]
  358. switch indexPath.row {
  359. //贴子用户
  360. case 0:
  361. let cell = CardContentUserTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  362. cell.communityRecommendDataModel = communityRecommendDataModel
  363. let shapeLayer = CAShapeLayer()
  364. cell.layer.mask = nil
  365. if indexPath.section == 0 {
  366. let bezierPath = UIBezierPath(roundedRect: cell.bounds,byRoundingCorners: [.topLeft,.topRight],cornerRadii: CGSize(width: 8,height: 8))
  367. shapeLayer.path = bezierPath.cgPath
  368. cell.layer.mask = shapeLayer
  369. }else {
  370. let bezierPath = UIBezierPath(roundedRect: cell.bounds,byRoundingCorners: [.allCorners],cornerRadii: CGSize(width: 0,height: 0))
  371. shapeLayer.path = bezierPath.cgPath
  372. cell.layer.mask = shapeLayer
  373. }
  374. cell.followClosure = {
  375. [weak self] in
  376. CommunityFollowUserViewModel.shared.follow(communityRecommendDataModel: communityRecommendDataModel, communityRecommendDataModels: (self?.communityRecommendDataModels)!, tableView: (self?.tableView)!)
  377. }
  378. cell.likeClosure = {[weak self] y in
  379. if UserModel.shared().isEqualUid(uid: communityRecommendDataModel.uid ?? 0) {
  380. CardContentUserDeleteView.sheetDeleteView(y: y, sureClosure: { [weak self] (cardContentUserDeleteView) in
  381. self?.communityDeleteApi(postId: communityRecommendDataModel.id ?? 0, section: indexPath.section)
  382. })
  383. }else {
  384. VirusViewModel.shared.like(communityRecommendDataModel: communityRecommendDataModel, y: y, tableView: (self?.tableView)!)
  385. }
  386. }
  387. return cell
  388. //图片视频
  389. case 1:
  390. let cell = CardContentPicVideoTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  391. cell.type = .recommend
  392. cell.communityRecommendDataModel = communityRecommendDataModel
  393. return cell
  394. //内容标题
  395. case 2:
  396. let cell = CardContentTitleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  397. cell.communityRecommendDataModel = communityRecommendDataModel
  398. return cell
  399. //点赞,收藏,分享
  400. case 3:
  401. let cell = CardContentActionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  402. cell.communityRecommendDataModel = communityRecommendDataModel
  403. cell.praiseClosureName = {
  404. [weak self] (button:UIButton) in
  405. VirusViewModel.shared.praise(communityRecommendDataModel: communityRecommendDataModel, tableView: (self?.tableView)!)
  406. }
  407. cell.collectClosureName = {
  408. [weak self] (button:UIButton) in
  409. VirusViewModel.shared.collection(communityRecommendDataModel: communityRecommendDataModel, tableView: (self?.tableView)!)
  410. }
  411. return cell
  412. //评论列表
  413. case 4:
  414. let cell = CardContentCommentListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  415. cell.communityRecommendDataModel = communityRecommendDataModel
  416. return cell
  417. //评论
  418. case 5:
  419. let cell = CardContentCommnetTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  420. cell.addCommnetClosureName = {
  421. [weak self] in
  422. self?.communityRecommendDataModel = self?.communityRecommendDataModels[indexPath.section]
  423. self?.showKeyBoardCommentView()
  424. }
  425. cell.isAvatar = true
  426. return cell
  427. default:
  428. return UITableViewCell()
  429. }
  430. }
  431. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  432. let communityRecommendDataModel = communityRecommendDataModels[indexPath.section]
  433. switch indexPath.row {
  434. //贴子用户
  435. case 0:
  436. break
  437. //图片视频/内容标题/评论列表
  438. case 1,2:
  439. if PostType(rawValue: communityRecommendDataModel.type ?? "video") == .video {
  440. Mediator.push(CommunityRouterModuleType.pushPostDetailVoide(postId: "\(communityRecommendDataModel.id ?? 0)", departType: .others, topicId: 0))
  441. }else {
  442. Mediator.push(CommunityRouterModuleType.pushPostDetailContent(postId: "\(communityRecommendDataModel.id ?? 0)"))
  443. }
  444. break
  445. //点赞,收藏,分享
  446. case 3:
  447. break
  448. //评论
  449. case 5:
  450. break
  451. default:
  452. break
  453. }
  454. }
  455. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  456. let communityRecommendDataModel = communityRecommendDataModels[indexPath.section]
  457. switch indexPath.row {
  458. //贴子用户
  459. case 0:
  460. if communityRecommendDataModel.uid != nil {
  461. return 80
  462. }
  463. return 0
  464. //图片视频
  465. case 1:
  466. let cardContentPicVideoModel = CardContentPicVideoModel()
  467. cardContentPicVideoModel.postType = PostType(rawValue: communityRecommendDataModel.type ?? "html")
  468. let imgStr = communityRecommendDataModel.img
  469. if communityRecommendDataModel.imgs?.count ?? 0 == 0 {
  470. if communityRecommendDataModel.img != "" && communityRecommendDataModel.img != nil {
  471. cardContentPicVideoModel.number = 1
  472. cardContentPicVideoModel.width = getImageWidth(imgStr: (imgStr)!)
  473. cardContentPicVideoModel.height = getImageHeight(imgStr: (imgStr)!)
  474. return cardContentPicVideoModel.collectionViewHeight() + 15
  475. }else {
  476. return 0
  477. }
  478. }else {
  479. if communityRecommendDataModel.imgs?.count ?? 0 == 1 {
  480. if communityRecommendDataModel.img != "" || communityRecommendDataModel.img != nil {
  481. cardContentPicVideoModel.number = 1
  482. cardContentPicVideoModel.width = getImageWidth(imgStr: (imgStr)!)
  483. cardContentPicVideoModel.height = getImageHeight(imgStr: (imgStr)!)
  484. return cardContentPicVideoModel.collectionViewHeight() + 15
  485. }else {
  486. return 0
  487. }
  488. }else {
  489. cardContentPicVideoModel.number = communityRecommendDataModel.imgs?.count ?? 0
  490. return cardContentPicVideoModel.collectionViewHeight() + 15
  491. }
  492. }
  493. //内容标题
  494. case 2:
  495. var str = communityRecommendDataModel.content ?? ""
  496. str = str.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "")
  497. if (communityRecommendDataModel.title == "" || communityRecommendDataModel.title == nil) {
  498. if str.count >= 45 {
  499. str = str.prefix(45) + "...更多"
  500. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  501. return contentHeight
  502. }else {
  503. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  504. return contentHeight
  505. }
  506. }else {
  507. if PostType(rawValue: communityRecommendDataModel.type ?? "html") == .html {
  508. return 22
  509. }else {
  510. if communityRecommendDataModel.content?.count ?? 0 >= 45 {
  511. str = str.prefix(45) + "...更多"
  512. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  513. return (22 + 6 + contentHeight)
  514. }else {
  515. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  516. return 22 + 6 + contentHeight
  517. }
  518. }
  519. }
  520. //点赞,收藏,分享
  521. case 3:
  522. return 54
  523. //评论列表
  524. case 4:
  525. if !(communityRecommendDataModel.comment?.isEmpty ?? true) {
  526. var height : CGFloat?
  527. for communityRecommendCommentModel in communityRecommendDataModel.comment!.prefix(2) {
  528. let nameStr = "\(communityRecommendCommentModel.username ?? ""):"
  529. let contentStr = "\(communityRecommendCommentModel.content ?? "")"
  530. let likeCountStr = "\(communityRecommendCommentModel.likeCount ?? 0)"
  531. let likeWidth = likeCountStr.widthForComment(font: kRegularFont14!, height: 25.5)
  532. let str = nameStr + contentStr
  533. let strHeight = (str.heightForComment(font: kRegularFont14!, width: kScreenWidth-28-10-16-5-likeWidth) + 5)
  534. height = (height ?? 0) + strHeight
  535. }
  536. if (communityRecommendDataModel.commentCount ?? 0) <= 2 {
  537. return (height ?? 0)
  538. }else {
  539. return 23 + (height ?? 0)
  540. }
  541. }
  542. return 0
  543. //评论
  544. case 5:
  545. return 50
  546. default:
  547. return 0
  548. }
  549. }
  550. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  551. return 0.000001
  552. }
  553. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  554. return 10
  555. }
  556. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  557. return nil
  558. }
  559. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  560. let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 10))
  561. view.backgroundColor = kf7f8faColor
  562. return view
  563. }
  564. }