123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- //
- // RecommendSimilarCell.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/6/14.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 推荐图文内容--相关推荐のCell
- import UIKit
- class RecommendSimilarCell: UITableViewCell {
-
- class func cellWith(tableView:UITableView,indexPath:IndexPath) -> RecommendSimilarCell {
- let ID = "RecommendSimilarCell"
- tableView.register(RecommendSimilarCell.self, forCellReuseIdentifier: ID)
- let cell : RecommendSimilarCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! RecommendSimilarCell
- 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
- backgroundColor = kf7f8faColor
- addSubview(collectionView)
- }
-
- private func setupLayouts() {
- collectionView.snp.makeConstraints { (make) in
- make.top.equalTo(0)
- make.left.equalTo(5)
- make.right.equalTo(-5)
- make.height.equalTo(200)
- }
- }
-
- private lazy var collectionView: UICollectionView = {
- let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
- collectionView.backgroundColor = kf7f8faColor
- collectionView.delegate = self;
- collectionView.dataSource = self;
- collectionView.showsVerticalScrollIndicator = false
- collectionView.showsHorizontalScrollIndicator = false
- collectionView.bounces = false
- collectionView.isScrollEnabled = false
- return collectionView
- }()
- private lazy var collectionViewLayout: WaterFallLayout = {
- let collectionViewLayout = WaterFallLayout()
- collectionViewLayout.delegate = self
- collectionViewLayout.scrollDirection = .horizontal
- return collectionViewLayout
- }()
-
- var collectionViewHeight : CGFloat?
- //加载数据
- func reloadData() {
- //collectionView重新加载数据
- self.collectionView.reloadData()
- //更新collectionView的高度约束
- let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
- collectionViewHeight = contentSize.height
- self.collectionView.snp.remakeConstraints { (make) in
- make.top.equalTo(0)
- make.left.equalTo(5)
- make.right.equalTo(-5)
- make.height.equalTo(contentSize.height)
- make.bottom.lessThanOrEqualToSuperview()
-
- }
- self.collectionView.collectionViewLayout.invalidateLayout()
- }
-
- var heights : Array<CGFloat>?
- var communityPostDataModels : Array<CommunityPostDataModel>? {
- didSet {
- self.collectionView.reloadData()
- }
- }
- var heightModel : HeightModel?{
- didSet {
- heightModel?.height = collectionViewHeight!
- }
- }
-
- }
- extension RecommendSimilarCell : WaterFallLayoutDelegate {
- func collectionView(_ collectionView: UICollectionView!, heightOfItemAt indexPath: IndexPath!) -> CGFloat {
- return heights?.isEmpty ?? true ? 0 : heights?[indexPath.row] ?? 0
- }
- func contentInsets(for collectionView: UICollectionView!) -> UIEdgeInsets {
- return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
- }
- func columenCounts(for collectionView: UICollectionView!) -> Int32 {
- return 2
- }
- func horizontalGap(for collectionView: UICollectionView!) -> CGFloat {
- return 5
- }
- func verticalGap(for collectionView: UICollectionView!) -> CGFloat {
- return 5
- }
- }
- extension RecommendSimilarCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
-
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
-
- }
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return communityPostDataModels?.isEmpty ?? true ? 0 : communityPostDataModels?.count ?? 0
-
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = SearchContentListCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
- cell.communityPostDataModel = communityPostDataModels?[indexPath.row]
- cell.userClosure = {
- [weak self] in
- if cell.communityPostDataModel?.uid != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = cell.communityPostDataModel?.uid ?? 0
- self?.findViewController().navigationController?.pushViewController(vc, animated: true)
- }
- }
- return cell
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- let communityPostDataModel = communityPostDataModels?[indexPath.row]
-
- if PostType(rawValue: communityPostDataModel?.type ?? "image") == .image || PostType(rawValue: communityPostDataModel?.type ?? "html") == .html {
- let vc = CommunityRecommendController()
- vc.id = communityPostDataModel?.id ?? 0
- findViewController().navigationController?.pushViewController(vc, animated: true)
- }else {
- let vc = CommunityVideoListController()
- vc.contentId = communityPostDataModel?.id ?? 0
- findViewController().navigationController?.pushViewController(vc, animated: true)
- }
-
-
- }
-
- }
|