// // HomeViewController.swift // MusicPlayer // // Created by Mr.Zhou on 2024/3/27. // import UIKit class MPSideA_HomeViewController: MPSideA_BaseViewController { @IBOutlet weak var tableView: UITableView!{ didSet{ if #available(iOS 15.0, *) { tableView.sectionHeaderTopPadding = 0 } tableView.contentInsetAdjustmentBehavior = .never tableView.estimatedRowHeight = 200 tableView.rowHeight = UITableView.automaticDimension tableView.tableHeaderView = headView //额外加载一段下边距 tableView.contentInset = .init(top: 0, left: 0, bottom: 100*width, right: 0) tableView.dataSource = self tableView.delegate = self tableView.register(UINib(nibName: Home_RowListsTableViewCellID, bundle: nil), forCellReuseIdentifier: Home_RowListsTableViewCellID) } } private lazy var headView:MPSideA_Home_HeadBannerView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 135*width)) private let Home_RowListsTableViewCellID = "MPSideA_Home_RowListsTableViewCell" //组头标题 private lazy var sectionTitles:[String] = ["", MPSideA_MusicShowType.First.title, MPSideA_MusicShowType.Second.title, MPSideA_MusicShowType.Third.title] override func viewDidLoad() { super.viewDidLoad() //触发音乐缺失闭包 nullMusicAction = { [weak self] in //刷新页面 self?.reload() } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) //添加通知 NotificationCenter.notificationKey.add(observer: self, selector: #selector(fileMissAction(_ :)), notificationName: .sideA_null_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .play_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .stop_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .resume_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .pause_music) reload() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) //移除所有通知 NotificationCenter.default.removeObserver(self) MP_AnalyticsManager.shared.home_a_pvAction() } deinit { } //刷新渲染页面 private func reload() { MPSideA_LoadDataMusic.shared.reloadListData() tableView.reloadData() } // //当音乐播放器状态改变时 // @objc private func switchPlayerStateAction(_ sender:Notification) { // //刷新页面内容 // tableView.reloadData() // } } //MARK: - tableView extension MPSideA_HomeViewController:UITableViewDataSource, UITableViewDelegate { func numberOfSections(in tableView: UITableView) -> Int { return sectionTitles.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: Home_RowListsTableViewCellID, for: indexPath) as! MPSideA_Home_RowListsTableViewCell cell.layout = .init(rawValue: indexPath.section) ?? .Zero return cell } //设置组头View func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let sectionView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 40*width)) sectionView.backgroundColor = .clear //添加一个Label let label = UILabel() label.textColor = .white label.font = .systemFont(ofSize: 20, weight: .regular) label.text = sectionTitles[section] sectionView.addSubview(label) label.snp.makeConstraints { make in make.left.equalToSuperview().offset(16*width) make.top.equalToSuperview() } return sectionView } //设置组头View高度 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return section != 0 ? 40*width:0 } }