// // MPPositive_HomeShowTableViewCell.swift // MusicPlayer // // Created by Mr.Zhou on 2024/4/22. // import UIKit ///b面主页Cell class MPPositive_HomeShowTableViewCell: UITableViewCell { //首页预览划分类型 enum HomeShowType:Int { case Frist = 0 case Second = 1 case Third = 2 case Fourth = 3 case Fifth = 4 ///对应布局layout var layout:UICollectionViewFlowLayout { let layout = UICollectionViewFlowLayout() layout.sectionInset = .init(top: 15*width, left: 16*width, bottom: 0, right: 16*width) layout.minimumLineSpacing = 8*width switch self { case .Frist: layout.itemSize = .init(width: 168*width, height: 134*width) layout.minimumInteritemSpacing = 0 layout.scrollDirection = .horizontal case .Second: layout.itemSize = .init(width: 96*width, height: 121*width) layout.minimumInteritemSpacing = 0 layout.scrollDirection = .horizontal case .Third: layout.itemSize = .init(width: 109*width, height: 152*width) layout.minimumInteritemSpacing = 0 layout.scrollDirection = .horizontal case .Fourth: layout.itemSize = .init(width: 64*width, height: 86*width) layout.minimumInteritemSpacing = 0 layout.scrollDirection = .horizontal case .Fifth: layout.itemSize = .init(width: 339*width, height: 237*width) layout.sectionInset = .init(top: 15*width, left: 16*width, bottom: 0, right: 20*width) layout.minimumInteritemSpacing = 16 layout.scrollDirection = .vertical } return layout } } //标题Label private lazy var titleLabel:UILabel = createLabel("Title", font: .systemFont(ofSize: 18*width, weight: .regular), textColor: .white, textAlignment: .left) //更多按钮 private lazy var moreBtn:UIButton = { let btn:UIButton = .init() btn.setBackgroundImage(UIImage(named: "Home_More'logo"), for: .normal) btn.addTarget(self, action: #selector(moreFindClick(_ :)), for: .touchUpInside) return btn }() //展示collectionView private lazy var collectionView:UICollectionView = { let collectionView:UICollectionView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: showType.layout) collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false collectionView.backgroundColor = .clear collectionView.dataSource = self collectionView.delegate = self collectionView.register(MPPositive_HomeListFirstCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFirstCollectionViewCellID) collectionView.register(MPPositive_HomeListSecondCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListSecondCollectionViewCellID) collectionView.register(MPPositive_HomeListThirdCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListThirdCollectionViewCellID) collectionView.register(MPPositive_HomeListFourthCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFourthCollectionViewCellID) collectionView.register(MPPositive_HomeListFifthCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFifthCollectionViewCellID) return collectionView }() private let MPPositive_HomeListFirstCollectionViewCellID = "MPPositive_HomeListFirstCollectionViewCell" private let MPPositive_HomeListSecondCollectionViewCellID = "MPPositive_HomeListSecondCollectionViewCell" private let MPPositive_HomeListThirdCollectionViewCellID = "MPPositive_HomeListThirdCollectionViewCell" private let MPPositive_HomeListFourthCollectionViewCellID = "MPPositive_HomeListFourthCollectionViewCell" private let MPPositive_HomeListFifthCollectionViewCellID = "MPPositive_HomeListFifthCollectionViewCell" ///预览模块 var browseViewModel:MPPositive_BrowseModuleListViewModel!{ didSet{ titleLabel.text = browseViewModel.title } } ///列表展示类型 var showType:HomeShowType = .Frist{ didSet{ collectionView.collectionViewLayout = showType.layout collectionView.reloadData() } } ///请求列表/专辑闭包 var requestNextBlock:((MPPositive_BrowseItemViewModel) -> Void)? ///查看更多闭包 var findMoreBlock:(() -> Void)? override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) confirgue() } required init?(coder: NSCoder) { super.init(coder: coder) } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } //配置 private func confirgue() { selectionStyle = .none backgroundColor = .clear contentView.backgroundColor = .clear //配置UI contentView.addSubview(moreBtn) moreBtn.snp.makeConstraints { make in make.right.equalToSuperview().offset(-16*width) make.top.equalToSuperview() make.width.height.equalTo(24*width) } contentView.addSubview(titleLabel) titleLabel.snp.makeConstraints { make in make.left.equalToSuperview().offset(16*width) make.bottom.equalTo(moreBtn.snp.bottom) make.right.equalTo(moreBtn.snp.left).offset(-10*width) } contentView.addSubview(collectionView) collectionView.snp.makeConstraints { make in make.top.equalTo(moreBtn.snp.bottom) make.left.right.equalToSuperview() make.bottom.equalToSuperview().offset(-32*width).priority(999) } } //查看更多 @objc private func moreFindClick(_ sender:UIButton) { guard findMoreBlock != nil else { return } findMoreBlock!() } //经过计算设置tableViewCell真实高度 override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize { let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority) collectionView.layoutIfNeeded() switch showType { case .Fifth: //纵向 let height = collectionView.collectionViewLayout.collectionViewContentSize.height return CGSize(width: size.width, height: size.height + height) default: //横向 let height = showType.layout.itemSize.height + showType.layout.sectionInset.top + showType.layout.sectionInset.bottom return CGSize(width: size.width, height: size.height + height) } } } //MARK: - collectionView extension MPPositive_HomeShowTableViewCell:UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return browseViewModel.items.count > 5 ? 5:browseViewModel.items.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch showType { case .Frist: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFirstCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFirstCollectionViewCell cell.itemViewModel = browseViewModel.items[indexPath.row] return cell case .Second: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListSecondCollectionViewCellID, for: indexPath) as! MPPositive_HomeListSecondCollectionViewCell cell.itemViewModel = browseViewModel.items[indexPath.row] return cell case .Third: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListThirdCollectionViewCellID, for: indexPath) as! MPPositive_HomeListThirdCollectionViewCell cell.itemViewModel = browseViewModel.items[indexPath.row] return cell case .Fourth: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFourthCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFourthCollectionViewCell cell.itemViewModel = browseViewModel.items[indexPath.row] return cell case .Fifth: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFifthCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFifthCollectionViewCell cell.itemViewModel = browseViewModel.items[indexPath.row] return cell } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard requestNextBlock != nil else { return } requestNextBlock!(browseViewModel.items[indexPath.row]) } }