ProHUD/Example-Old/Example/BaseListVC.swift

107 lines
3.1 KiB
Swift
Raw Permalink Normal View History

2019-08-12 17:59:40 +08:00
//
// BaseListVC.swift
// Example
//
// Created by xaoxuu on 2019/8/12.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
2020-06-23 20:16:12 +08:00
typealias Callback = () -> Void
struct Table {
var sections = [Section]()
mutating func addSection(title: String, callback: @escaping (inout Section) -> Void) {
var sec = Section()
sec.header = title
callback(&sec)
sections.append(sec)
}
}
struct Section {
var header = ""
var footer = ""
var rows = [Row]()
mutating func addRow(title: String, subtitle: String? = nil, callback: @escaping Callback) {
rows.append(Row(title: title, subtitle: subtitle, callback: callback))
}
}
struct Row {
var title: String
var subtitle: String?
var callback: Callback
}
2019-08-12 17:59:40 +08:00
class BaseListVC: UIViewController {
2020-06-23 20:16:12 +08:00
var ts = [[String]]()
var cs = [[Callback]]()
var vm = Table()
var secs = [Section]()
2019-08-12 17:59:40 +08:00
lazy var tableView: UITableView = {
2021-07-20 16:04:39 +08:00
let tv: UITableView
if #available(iOS 13.0, *) {
tv = UITableView(frame: .zero, style: .insetGrouped)
} else {
// Fallback on earlier versions
tv = UITableView(frame: .zero, style: .grouped)
}
2019-08-12 17:59:40 +08:00
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.dataSource = self
tableView.delegate = self
tableView.snp.makeConstraints { (mk) in
mk.edges.equalToSuperview()
}
}
2020-06-23 20:16:12 +08:00
2019-08-12 17:59:40 +08:00
}
extension BaseListVC: UITableViewDataSource, UITableViewDelegate {
2020-06-23 20:16:12 +08:00
func numberOfSections(in tableView: UITableView) -> Int {
return vm.sections.count
}
2019-08-12 17:59:40 +08:00
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2020-06-23 20:16:12 +08:00
return vm.sections[section].rows.count
2019-08-12 17:59:40 +08:00
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2020-06-23 20:16:12 +08:00
let cell: UITableViewCell
if let c = tableView.dequeueReusableCell(withIdentifier: "cell") {
cell = c
} else {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.textColor = .gray
cell.detailTextLabel?.numberOfLines = 0
2020-06-24 13:27:50 +08:00
cell.accessoryType = .disclosureIndicator
2020-06-23 20:16:12 +08:00
}
cell.textLabel?.text = vm.sections[indexPath.section].rows[indexPath.row].title
cell.detailTextLabel?.text = vm.sections[indexPath.section].rows[indexPath.row].subtitle
2019-08-12 17:59:40 +08:00
return cell
}
2020-06-23 20:16:12 +08:00
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return vm.sections[section].header
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return vm.sections[section].footer
}
2019-08-12 17:59:40 +08:00
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
2020-06-23 20:16:12 +08:00
vm.sections[indexPath.section].rows[indexPath.row].callback()
2019-08-12 17:59:40 +08:00
}
}
2020-06-23 20:16:12 +08:00