-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
133 lines (108 loc) · 3.88 KB
/
ViewController.swift
File metadata and controls
133 lines (108 loc) · 3.88 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
//
// ViewController.swift
// ModuleRouteExample
//
// Created by GIKI on 2025/2/14.
//
import UIKit
import ModuleRoute
class ViewController: UIViewController {
@MRInject var navigator: MRNavigator
// @Inject(DetailInterface) var detail: DetailInterface
// MARK: - Properties
private var collectionView: UICollectionView!
private let cellIdentifier = "Cell"
// 可配置的数据源
private var items: [ItemModel] = [
ItemModel(title: "Item 1", color: .blue),
ItemModel(title: "Item 2", color: .green),
ItemModel(title: "Item 3", color: .orange),
ItemModel(title: "Item 4", color: .purple)
]
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
// MARK: - Setup
private func setupCollectionView() {
// 创建布局
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: (view.frame.width - 40) / 2, height: 100)
layout.minimumInteritemSpacing = 10
layout.minimumLineSpacing = 10
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
// 初始化 CollectionView
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.backgroundColor = .white
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// 注册 cell
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: cellIdentifier)
// 设置代理
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
}
}
// MARK: - UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CustomCell
let item = items[indexPath.item]
cell.configure(with: item)
return cell
}
}
// MARK: - UICollectionViewDelegate
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = items[indexPath.item]
if indexPath.item == 1 {
@MRInject var detail: DetailInterface
let result = detail.handle(route: DetailRoute())
switch result {
case .navigator(let vc):
self.navigationController?.pushViewController(vc, animated: true)
default: break
}
print(result)
} else {
navigator.navigate(to: ChatRoute(), from: self)
}
}
}
// MARK: - Custom Cell
class CustomCell: UICollectionViewCell {
private let titleLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.textColor = .white
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
contentView.addSubview(titleLabel)
titleLabel.frame = contentView.bounds
titleLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
func configure(with item: ItemModel) {
titleLabel.text = item.title
contentView.backgroundColor = item.color
layer.cornerRadius = 8
clipsToBounds = true
}
}
// MARK: - Model
struct ItemModel {
let title: String
let color: UIColor
}