|
@@ -0,0 +1,325 @@
|
|
|
+//
|
|
|
+// CommunityRecommendDetailHTMLTableViewCell.swift
|
|
|
+// RainbowPlanet
|
|
|
+//
|
|
|
+// Created by 南鑫林 on 2019/7/4.
|
|
|
+// Copyright © 2019 RainbowPlanet. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import UIKit
|
|
|
+import WebKit
|
|
|
+
|
|
|
+class CommunityRecommendDetailHTMLTableViewCell: UITableViewCell {
|
|
|
+
|
|
|
+ class func cellWith(tableView:UITableView,indexPath:IndexPath) -> UITableViewCell {
|
|
|
+ let ID = "CommunityRecommendDetailHTMLTableViewCell"
|
|
|
+ tableView.register(CommunityRecommendDetailHTMLTableViewCell.self, forCellReuseIdentifier: ID)
|
|
|
+ let cell : CommunityRecommendDetailHTMLTableViewCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! CommunityRecommendDetailHTMLTableViewCell
|
|
|
+ cell.indexPath = indexPath
|
|
|
+ return cell
|
|
|
+ }
|
|
|
+
|
|
|
+ override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
+ super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
+ setupViews()
|
|
|
+ setupLayouts()
|
|
|
+ }
|
|
|
+
|
|
|
+ required init?(coder aDecoder: NSCoder) {
|
|
|
+ fatalError("init(coder:) has not been implemented")
|
|
|
+ }
|
|
|
+
|
|
|
+ var indexPath: IndexPath? {
|
|
|
+ didSet {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //MRAK: - 设置View
|
|
|
+ private func setupViews() {
|
|
|
+ self.selectionStyle = .none
|
|
|
+ addSubview(titleLabel)
|
|
|
+ addSubview(avatarbgView)
|
|
|
+ avatarbgView.addSubview(avatarButton)
|
|
|
+ avatarbgView.addSubview(nameButton)
|
|
|
+ avatarbgView.addSubview(followButton)
|
|
|
+ addSubview(beanImageView)
|
|
|
+ addSubview(beanLabel)
|
|
|
+ addSubview(timeReadLabel)
|
|
|
+ addSubview(webView)
|
|
|
+ addSubview(collectionView)
|
|
|
+ addSubview(locationButton)
|
|
|
+ }
|
|
|
+
|
|
|
+ private func setupLayouts() {
|
|
|
+ titleLabel.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(10)
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ make.left.equalTo(14)
|
|
|
+ }
|
|
|
+ avatarbgView.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(titleLabel.snp_bottom).offset(10)
|
|
|
+ make.left.right.equalToSuperview()
|
|
|
+ make.height.equalTo(44)
|
|
|
+ }
|
|
|
+ avatarButton.snp.makeConstraints { (make) in
|
|
|
+ make.left.equalTo(14)
|
|
|
+ make.centerY.equalToSuperview()
|
|
|
+ make.size.equalTo(30)
|
|
|
+ }
|
|
|
+ nameButton.snp.makeConstraints { (make) in
|
|
|
+ make.centerY.equalToSuperview()
|
|
|
+ make.left.equalTo(avatarButton.snp.right).offset(5)
|
|
|
+ }
|
|
|
+ followButton.snp.makeConstraints { (make) in
|
|
|
+ make.centerY.equalToSuperview()
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ make.height.equalTo(26)
|
|
|
+ make.width.equalTo(64)
|
|
|
+ }
|
|
|
+ beanLabel.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(avatarbgView.snp_bottom).offset(10)
|
|
|
+ make.left.equalTo(32)
|
|
|
+ make.height.equalTo(16)
|
|
|
+ }
|
|
|
+ beanImageView.snp.makeConstraints { (make) in
|
|
|
+ make.centerY.equalTo(beanLabel)
|
|
|
+ make.right.equalTo(beanLabel.snp_left).offset(-3)
|
|
|
+ }
|
|
|
+ timeReadLabel.snp.makeConstraints { (make) in
|
|
|
+ make.centerY.equalTo(beanLabel)
|
|
|
+ make.left.equalTo(beanLabel.snp.right)
|
|
|
+ }
|
|
|
+ webView.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(beanLabel.snp_bottom).offset(20)
|
|
|
+ make.left.equalTo(14)
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ make.height.equalTo(10)
|
|
|
+ }
|
|
|
+ collectionView.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(webView.snp_bottom).offset(15)
|
|
|
+ make.left.equalTo(14)
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ make.height.equalTo(10)
|
|
|
+ }
|
|
|
+ locationButton.snp.makeConstraints { (make) in
|
|
|
+ make.left.equalTo(-14)
|
|
|
+ make.top.equalTo(collectionView.snp_bottom).offset(15)
|
|
|
+ make.height.equalTo(15)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private lazy var titleLabel: UILabel = {
|
|
|
+ let titleLabel = UILabel()
|
|
|
+ titleLabel.textColor = k262626Color
|
|
|
+ titleLabel.font = kMediumFont16
|
|
|
+ titleLabel.textAlignment = .left
|
|
|
+ titleLabel.numberOfLines = 0
|
|
|
+ return titleLabel
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var avatarbgView: UIView = {
|
|
|
+ let avatarbgView = UIView()
|
|
|
+ return avatarbgView
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var avatarButton: UIButton = {
|
|
|
+ let avatarButton = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ avatarButton.setImage(kImage(name: "default_avatar"), for: UIControl.State.normal)
|
|
|
+ avatarButton.cornerRadius = 15
|
|
|
+ avatarButton.masksToBounds = true
|
|
|
+ return avatarButton
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var nameButton: UIButton = {
|
|
|
+ let nameButton = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ nameButton.setTitle("昵称", for: UIControl.State.normal)
|
|
|
+ nameButton.setTitleColor(k262626Color, for: UIControl.State.normal)
|
|
|
+ nameButton.titleLabel?.font = kRegularFont14
|
|
|
+ return nameButton
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var followButton: UIButton = {
|
|
|
+ let followButton = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ followButton.titleLabel?.font = kMediumFont13
|
|
|
+ followButton.cornerRadius = 12
|
|
|
+ followButton.masksToBounds = true
|
|
|
+ followButton.layer.borderWidth = 0.5
|
|
|
+ followButton.isHidden = true
|
|
|
+ return followButton
|
|
|
+ }()
|
|
|
+
|
|
|
+ private lazy var beanImageView : UIImageView = {
|
|
|
+ let beanImageView = UIImageView()
|
|
|
+ beanImageView.image = kImage(name: "navbar_bean_org")
|
|
|
+ return beanImageView
|
|
|
+ }()
|
|
|
+
|
|
|
+ private lazy var beanLabel: UILabel = {
|
|
|
+ let beanLabel = UILabel()
|
|
|
+ beanLabel.textColor = kFFA42FColor
|
|
|
+ beanLabel.font = kRegularFont13
|
|
|
+ beanLabel.textAlignment = .left
|
|
|
+ return beanLabel
|
|
|
+ }()
|
|
|
+
|
|
|
+ private lazy var timeReadLabel: UILabel = {
|
|
|
+ let timeReadLabel = UILabel()
|
|
|
+ timeReadLabel.textColor = kbbbbbbColor
|
|
|
+ timeReadLabel.font = kRegularFont12
|
|
|
+ timeReadLabel.textAlignment = .left
|
|
|
+ return timeReadLabel
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var webView: WKWebView = {
|
|
|
+ let webView = WKWebView()
|
|
|
+// webView.scrollView.isScrollEnabled = false
|
|
|
+ webView.scrollView.bounces = false
|
|
|
+ webView.scrollView.showsVerticalScrollIndicator = false
|
|
|
+ webView.autoresizingMask = UIView.AutoresizingMask.flexibleHeight
|
|
|
+ webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)
|
|
|
+ return webView
|
|
|
+ }()
|
|
|
+
|
|
|
+ private lazy var collectionView: UICollectionView = {
|
|
|
+ let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
|
|
|
+ collectionView.backgroundColor = kffffffColor
|
|
|
+ collectionView.delegate = self
|
|
|
+ collectionView.dataSource = self
|
|
|
+ collectionView.showsVerticalScrollIndicator = false
|
|
|
+ collectionView.showsHorizontalScrollIndicator = false
|
|
|
+ collectionView.bounces = false
|
|
|
+ collectionView.isScrollEnabled = false
|
|
|
+ return collectionView
|
|
|
+ }()
|
|
|
+
|
|
|
+ private lazy var collectionViewLayout: UICollectionViewLeftAlignedLayout = {
|
|
|
+ let collectionViewLayout = UICollectionViewLeftAlignedLayout.init()
|
|
|
+ collectionViewLayout.minimumLineSpacing = 10
|
|
|
+ collectionViewLayout.minimumInteritemSpacing = 10
|
|
|
+ collectionViewLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
|
|
|
+ collectionViewLayout.estimatedItemSize = CGSize(width: ((kScreenWidth - 28) - 30)/4, height: 24)
|
|
|
+ collectionViewLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
|
|
|
+ return collectionViewLayout
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var locationButton: UIButton = {
|
|
|
+ let locationButton = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ locationButton.setImage(kImage(name: "btn_address"), for: UIControl.State.normal)
|
|
|
+ locationButton.setTitleColor(kbbbbbbColor, for: UIControl.State.normal)
|
|
|
+ locationButton.titleLabel?.font = kRegularFont12
|
|
|
+ return locationButton
|
|
|
+ }()
|
|
|
+
|
|
|
+ var collectionViewheight : CGFloat = 0
|
|
|
+ //加载数据
|
|
|
+ func reloadData() {
|
|
|
+ //collectionView重新加载数据
|
|
|
+ self.collectionView.reloadData()
|
|
|
+ //更新collectionView的高度约束
|
|
|
+ let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
|
|
|
+ collectionViewheight = contentSize.height
|
|
|
+ collectionView.snp.remakeConstraints { (make) in
|
|
|
+ make.top.equalTo(webView.snp_bottom).offset(15)
|
|
|
+ make.left.equalTo(14)
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ make.height.equalTo(contentSize.height)
|
|
|
+ make.bottom.lessThanOrEqualToSuperview()
|
|
|
+ }
|
|
|
+ self.collectionView.collectionViewLayout.invalidateLayout()
|
|
|
+ }
|
|
|
+
|
|
|
+ var communityPostDetailModel : CommunityPostDetailModel? {
|
|
|
+ didSet {
|
|
|
+
|
|
|
+ avatarButton.kf.setImage(with: kURLImage(name: communityPostDetailModel?.avatar ?? ""), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
|
|
|
+ nameButton.setTitle(communityPostDetailModel?.username, for: UIControl.State.normal)
|
|
|
+ CommunityFollowUserViewModel.shared.setFollowType(followButton: followButton, followType: FollowType(rawValue: communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
|
|
|
+ beanLabel.text = "\(communityPostDetailModel?.availableBean ?? 0)彩虹豆待收获"
|
|
|
+ timeReadLabel.text = "I \(communityPostDetailModel?.createdAt ?? "") I 阅读·\(communityPostDetailModel?.pv ?? 0)"
|
|
|
+ webView.load(URLRequest(url: URL(string: communityPostDetailModel?.descUrl ?? "")!))
|
|
|
+
|
|
|
+
|
|
|
+ if communityPostDetailModel?.title == nil || communityPostDetailModel?.title == "" {
|
|
|
+ avatarbgView.snp.remakeConstraints { (make) in
|
|
|
+ make.top.equalTo(0)
|
|
|
+ make.height.equalTo(44)
|
|
|
+ make.left.right.equalToSuperview()
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ titleLabel.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(10)
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ make.left.equalTo(14)
|
|
|
+ }
|
|
|
+ avatarbgView.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalTo(titleLabel.snp_bottom).offset(10)
|
|
|
+ make.left.right.equalToSuperview()
|
|
|
+ make.height.equalTo(44)
|
|
|
+ }
|
|
|
+ titleLabel.text = communityPostDetailModel?.title
|
|
|
+ }
|
|
|
+
|
|
|
+ if communityPostDetailModel?.location == nil || communityPostDetailModel?.location == "" {
|
|
|
+ locationButton.isHidden = true
|
|
|
+ }else {
|
|
|
+ locationButton.isHidden = false
|
|
|
+ locationButton.setTitle(communityPostDetailModel?.location, for: UIControl.State.normal)
|
|
|
+ locationButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 3)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var webViewCellHeight : CGFloat?
|
|
|
+
|
|
|
+ var heightModel : HeightModel? {
|
|
|
+ didSet {
|
|
|
+ let contentLabelHeight = communityPostDetailModel?.content?.heightForComment(font: kRegularFont15!, width: kScreenWidth - 28)
|
|
|
+ let subViewHeight = 24.0 + (contentLabelHeight ?? 0) + collectionViewheight + 15.0
|
|
|
+ if communityPostDetailModel?.title == nil || communityPostDetailModel?.title == "" {
|
|
|
+
|
|
|
+ let spacHeight = 20.0 + 10.0 + 15.0 + 15.0 + 20.0
|
|
|
+ heightModel?.height = subViewHeight + CGFloat(spacHeight)
|
|
|
+ }else {
|
|
|
+ let titleHeight = communityPostDetailModel?.title?.heightForComment(font: kMediumFont16!, width: kScreenWidth - 28)
|
|
|
+ let spacHeight = 20.0 + 10.0 + 10.0 + 15.0 + 15.0 + 20.0
|
|
|
+ heightModel?.height = subViewHeight + CGFloat(spacHeight) + (titleHeight ?? 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+extension CommunityRecommendDetailHTMLTableViewCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
|
|
|
+ func numberOfSections(in collectionView: UICollectionView) -> Int {
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
+ return communityPostDetailModel?.topic?.count ?? 0
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
+ let cell = CommunityTagCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
|
|
|
+ cell.communityPostDetailTopicModel = communityPostDetailModel?.topic?[indexPath.row]
|
|
|
+ return cell
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
+ let communityPostDetailTopicModel = communityPostDetailModel?.topic?[indexPath.row]
|
|
|
+ if let id = communityPostDetailTopicModel?.id {
|
|
|
+ let vc = CommunityFeaturedTopicsViewController()
|
|
|
+ vc.id = id
|
|
|
+ findViewController().navigationController?.pushViewController(vc, animated: true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension CommunityRecommendDetailHTMLTableViewCell {
|
|
|
+ override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
|
|
+ if keyPath == "contentSize" {
|
|
|
+ webViewCellHeight = self.webView.scrollView.contentSize.height
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|