Visual_Novel_iOS/crush/Crush/Src/Modules/TestEntrances/Data/TestDatasTableController.swift

120 lines
3.5 KiB
Swift

//
// 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")
}
}