Repository navigation
Expand file tree
/
Copy pathCYLSettingsDemoTableViewController.swift
More file actions
144 lines (122 loc) · 6.66 KB
/
Copy pathCYLSettingsDemoTableViewController.swift
File metadata and controls
144 lines (122 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// CYLSettingsDemoTableViewController.swift
// CYLTabBarController demos
//
// Copyright © 2026 https://github.com/ChenYilong . All rights reserved.
//
import CYLTabBarController
import UIKit
/*!
* [EN] The ObjC demo's test list in pure Swift: every settings row of the library (language, appearance, in-place tab item
* updates, pages and login, the plus button, tab interactions, helpers), grouped like Settings. The rows, their cells
* and their actions come from the library (`UIViewController (CYLSettingsSection)`), so this list only lays them out.
* Shared by the Swift demos (Example-Rive, Example-SwiftUI-Rive, Example-Pag). Optional `headerView` goes above the
* rows, e.g. a demo of the page's own feature.
* [CN] ObjC Demo 测试列表的纯 Swift 版本:库中所有设置行(语言、外观、原地更新 Tab、页面与登录、PlusButton、Tab 交互、工具方法),像系统设置一样分组。
* 行、cell 与点击后的操作都来自库(`UIViewController (CYLSettingsSection)`),本列表只负责布局。由各 Swift Demo 共用(Example-Rive、
* Example-SwiftUI-Rive、Example-Pag)。可选的 `headerView` 显示在各行上方,例如该页面自身功能的演示。
*/
class CYLSettingsDemoTableViewController: UITableViewController {
/// [EN] Shown above the rows. [CN] 显示在各行上方。
var headerView: UIView? {
didSet { layoutHeaderView() }
}
/// [EN] The library's groups that have rows, read once per reload. [CN] 有行的库分组,每次刷新时读取。
private var groups: [CYLSettingsGroup] = []
init() {
super.init(style: .insetGrouped)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) is not supported")
}
override func viewDidLoad() {
super.viewDidLoad()
UITableViewCell.cylSetUpAutomaticRowHeight(for: tableView)
reloadGroups()
layoutHeaderView()
// [EN] The skin row shows the current theme, and a theme is picked while this list stays on screen (#216).
// [CN] 换肤行显示当前主题,而选择主题时本列表一直在屏幕上(#216)。
view.cyl_themeDidChangeBlock = { [weak self] in
self?.reloadGroups()
}
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
addTabBarStyleSwitch()
}
/// [EN] Only in the demos that can rebuild their tab bar in the other style. A list shown inside a page (Example-PAG) adds it
/// to that page's navigation bar, next to the page's own buttons.
/// [CN] 仅在能以另一种样式重建 TabBar 的 Demo 中显示。显示在页面内部的列表(Example-PAG)把它添加到该页面的导航栏,排在页面自己的按钮旁边。
private func addTabBarStyleSwitch() {
guard CYLDemoTabBarStyle.rebuildRootViewController != nil else { return }
let host = parent.flatMap { $0 is UINavigationController ? nil : $0 } ?? self
let item = UIBarButtonItem(title: CYLDemoTabBarStyle.switchTitle, style: .plain, target: self, action: #selector(switchTabBarStyle))
var items = host.navigationItem.rightBarButtonItems ?? []
items.removeAll { $0.action == #selector(switchTabBarStyle) }
host.navigationItem.rightBarButtonItems = items + [item]
}
@objc private func switchTabBarStyle() {
CYLDemoTabBarStyle.toggle(from: self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// [EN] Rows show state that other tabs can change (login, titles, icons). [CN] 各行显示的状态可能被其他 Tab 修改(登录、标题、图标)。
reloadGroups()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
layoutHeaderView()
}
private func reloadGroups() {
groups = cylSettingsGroups().compactMap { CYLSettingsGroup(rawValue: $0.intValue) }
tableView.reloadData()
}
/// [EN] A table header keeps the frame it is given: size it to fit the table's width. Called from
/// `viewDidLayoutSubviews`, so it changes the header only when something changed: setting it lays the table out
/// again, which would call this again, forever.
/// [CN] 表头保持设置的 frame:按列表宽度计算其大小。由 `viewDidLayoutSubviews` 调用,因此只在有变化时才修改表头:设置表头会让列表
/// 重新布局并再次调用这里,导致无限循环。
private func layoutHeaderView() {
guard isViewLoaded else { return }
guard let headerView = headerView else {
if tableView.tableHeaderView != nil {
tableView.tableHeaderView = nil
}
return
}
let width = tableView.bounds.width
let height = headerView.systemLayoutSizeFitting(CGSize(width: width, height: UIView.layoutFittingCompressedSize.height),
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel).height
if tableView.tableHeaderView !== headerView || headerView.frame.size != CGSize(width: width, height: height) {
headerView.frame = CGRect(x: 0, y: 0, width: width, height: height)
tableView.tableHeaderView = headerView
}
}
private func settingsRow(at indexPath: IndexPath) -> Int {
cylSettingsRows(in: groups[indexPath.section])[indexPath.row].intValue
}
// MARK: - UITableViewDataSource
override func numberOfSections(in _: UITableView) -> Int {
groups.count
}
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
cylSettingsRows(in: groups[section]).count
}
override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
cylTitle(for: groups[section])
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cylSettingsCell(for: tableView, row: settingsRow(at: indexPath))
}
// MARK: - UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
cylDidSelectSettingsRow(settingsRow(at: indexPath))
// [EN] The row may have switched a tab, so this list may no longer be on screen. [CN] 该行可能已切换 Tab,本列表可能已不在屏幕上。
if tableView.window != nil {
tableView.reloadData()
}
}
}