// // TestDatasTableController.swift // Crush // // Created by Leon on 2025/7/15. // import Foundation import UIKit // MARK: - 配置模型 struct TableViewConfig { // 数据模型 struct Item { let title: String let subtitle: String? let action: () -> Void // 点击单元格执行的方法 } let items: [Item] // 数据源 let cellIdentifier: String // 单元格重用标识 let rowHeight: CGFloat // 行高 let configureCell: (UITableViewCell, Item) -> Void // 配置单元格的闭包 } // MARK: - 通用 TableView 数据源和代理 class TestDatasTableHandler: NSObject, UITableViewDataSource, UITableViewDelegate { private let config: TableViewConfig init(config: TableViewConfig) { self.config = config } // MARK: - UITableViewDataSource func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return config.items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: config.cellIdentifier, for: indexPath) let item = config.items[indexPath.row] config.configureCell(cell, item) return cell } // MARK: - UITableViewDelegate func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return config.rowHeight } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) let item = config.items[indexPath.row] item.action() } } // MARK: - TestDatasTableController class TestDatasTableController: CLBaseViewController { private var tableView: UITableView = UITableView() private var handler: TestDatasTableHandler! override func viewDidLoad() { super.viewDidLoad() title = "Test Datas" setupTableView() } private func setupTableView() { view.addSubview(tableView) tableView.snp.makeConstraints { make in make.top.equalTo(navigationView.snp.bottom) make.leading.trailing.bottom.equalToSuperview() // make.top.equalToSuperview() } tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") let config = TableViewConfig( items: [ .init(title: "Print iconfont", subtitle: "xxx", action: { [weak self] in self?.printIconFontEnum() }), .init(title: "Action 1", subtitle: "Execute first method", action: { [weak self] in self?.performAction1() }), .init(title: "Action 2", subtitle: "Execute second method", action: { [weak self] in self?.performAction2() }), .init(title: "Action 3", subtitle: nil, action: { [weak self] in self?.performAction3() }), ], cellIdentifier: "Cell", rowHeight: 44.0, configureCell: { cell, item in cell.textLabel?.text = item.title cell.detailTextLabel?.text = item.subtitle } ) handler = TestDatasTableHandler(config: config) tableView.dataSource = handler tableView.delegate = handler } private func printIconFontEnum() { MWIconFont.printIconCodeEnum() } private func performAction1() { print("Performed Action 1") } private func performAction2() { print("Performed Action 2") } private func performAction3() { print("Performed Action 3") } }