// // 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? var communityPostDataModels : Array? { 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) } } }