// // SPHomeViewController.swift // ShortPlay // // Created by 曾觉新 on 2025/4/8. // import UIKit class SPHomeViewController: SPHomeChildController { ///模版数据 private var moduleModel: SPHomeModuleModel? private lazy var layout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() return layout }() private lazy var collectionView: SPCollectionView = { let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: layout) collectionView.delegate = self collectionView.dataSource = self collectionView.register(SPHomeHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView") SPCollectionViewCell.registerCell(collectionView: collectionView) return collectionView }() override func viewDidLoad() { super.viewDidLoad() // view.backgroundColor = .clear requestModuleData() _setupUI() } override func fetchChildControllerScrollView() -> UIScrollView? { return self.collectionView } } extension SPHomeViewController { private func _setupUI() { view.addSubview(self.collectionView); self.collectionView.snp.makeConstraints { make in make.left.right.bottom.equalToSuperview() make.top.equalToSuperview().offset(topMargins) } } } //MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource -------------- extension SPHomeViewController: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = SPCollectionViewCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath) return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 0 } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { if let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath) as? SPHomeHeaderView { headerView.moduleModel = self.moduleModel return headerView } return UICollectionReusableView() } } extension SPHomeViewController { private func requestModuleData() { SPHomeAPI.requestHomeModuleData { [weak self] model in guard let self = self else { return } if let model = model { self.moduleModel = model self.layout.headerReferenceSize = CGSize(width: kSPScreenWidth, height: SPHomeHeaderView.contentHeight(model: model)) self.collectionView.reloadData() } } } }