Music_Player3/MusicPlayer/MP/Common/Tool(工具封装)/MPTableManager.swift
Mr.zhou 96147c5e37 项目:Musicoo
版本:A面 1.0
构建:1.1
更新内容:对项目A面功能的实现,经测试确定各项功能无问题。
更新时间:2024年4月12日 11:20
上传状态:已上传App Connect
2024-04-12 11:19:58 +08:00

101 lines
3.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MPTableManager.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/7.
//
import UIKit
///tableViewCell(index-tableView-VCcell)
typealias TableViewCellConfigureBlock = (_ index:IndexPath, _ tableView:UITableView) -> UITableViewCell
///tableViewCell(index-tableView-VC)
typealias TableViewCellDidSelectBlock = (_ index:IndexPath, _ tableView:UITableView) -> Void
///tableView
class MPTableManager:NSObject {
///
private var sectionAndItems:[[Any]]?
///cellid()
private var cellIdentifiers:[String]?
//
private var configureCellBlock:TableViewCellConfigureBlock!
//
private var didSelectBlock:TableViewCellDidSelectBlock?
/// UITableView
/// - Parameters:
/// - sectionItems:
/// - cellIdentifier: cellIDsectionItems
/// - configureCellBlock: cell
/// - didSelectBlock: cell
init(_ sectionItems: [[Any]]?, cellIdentifiers: [String]?, configureCellBlock: TableViewCellConfigureBlock?, didSelectBlock: TableViewCellDidSelectBlock?) {
super.init()
self.sectionAndItems = sectionItems
self.cellIdentifiers = cellIdentifiers
self.configureCellBlock = configureCellBlock
self.didSelectBlock = didSelectBlock
}
//
private func item(_ indexPath: IndexPath) -> Any? {
if let items = sectionAndItems?[indexPath.section] {
return items[indexPath.row]
}else {
return nil
}
}
//tableViewdelegate,datasourcecell
func handleTableViewDataSourceAndDelegate(_ tableView: UITableView?, rowHeight:CGFloat = 0) {
guard let table = tableView else {
print("TableView is null")
return
}
table.dataSource = self
table.delegate = self
table.rowHeight = rowHeight != 0 ? rowHeight:UITableView.automaticDimension
table.estimatedRowHeight = 200
guard cellIdentifiers != nil else {
return
}
cellIdentifiers!.forEach { item in
//
guard table.dequeueReusableCell(withIdentifier: item) == nil else { return }
//nib
if let _ = Bundle.main.path(forResource: item, ofType: "nib") {
//nib
table.register(.init(nibName: item, bundle: nil), forCellReuseIdentifier: item)
}else {
//cell
table.register(item.classFromString().self, forCellReuseIdentifier: item)
}
}
}
}
//MARK: - tableView
extension MPTableManager:UITableViewDataSource, UITableViewDelegate {
//
func numberOfSections(in tableView: UITableView) -> Int {
guard sectionAndItems != nil else {
return 0
}
return sectionAndItems!.count
}
//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let items = sectionAndItems?[section] else {
return 0
}
return items.count
}
//cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = configureCellBlock(indexPath, tableView)
return cell
}
//
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard didSelectBlock != nil else {
return
}
didSelectBlock!(indexPath, tableView)
}
}