CNLiveShopClassilyManageViewController.swift
7.67 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// CNLiveShopClassilyManageViewController.swift
// metaCode
//
// Created by zx on 2023/12/12.
//
import Foundation
///区头id
let kCNLiveShopClassifySectionHeaderViewID = "kCNLiveShopClassifySectionHeaderViewID"
///cell ID
let kCNLiveShopClassilyManageTableViewCellID = "kCNLiveShopClassilyManageTableViewCellID"
class CNLiveShopClassilyManageViewController:CNLiveBaseViewController, UITableViewDataSource, UITableViewDelegate{
// MARK: - lazy
lazy var tableView : UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: KNavigationHeight, width: KSreenWidth, height: KSreenHeight-KNavigationHeight-KTabBarHeight), style: .plain)
tableView.backgroundColor = HexColor("#F6F6F6")
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
tableView.separatorStyle = .none
tableView.tableFooterView = UIView.init()
tableView.tableHeaderView = self.headerView
tableView.register(CNLiveShopClassifySectionHeaderView.self, forHeaderFooterViewReuseIdentifier: kCNLiveShopClassifySectionHeaderViewID)
tableView.register(CNLiveShopClassilyManageTableViewCell.self, forCellReuseIdentifier:kCNLiveShopClassilyManageTableViewCellID )
return tableView
}()
///表头
lazy var headerView :CNLiveShopClassilyManageHeaderView = {
let headerView = CNLiveShopClassilyManageHeaderView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: 114))
return headerView
}()
lazy var bottomNavView :UIView = {
let bottomNavView = UIView(frame: CGRect(x: 0, y: KSreenHeight-KTabBarHeight, width: KSreenWidth, height: KTabBarHeight))
bottomNavView.backgroundColor = .white
return bottomNavView
}()
///分类列表详情数据
var classifyListModel:CNLiveShopClassifyListModel?
///一级分类数据源,一级分类里面包含了二级分类
var listArray:[CNLiveShopClassifyModel]?
override func viewDidLoad() {
super.viewDidLoad()
self.initUI()
self.loadData()
}
// MARK: - 初始化UI
func initUI(){
self.view.addSubview(self.tableView)
self.initTopNav()
self.initBottomNav()
}
func initTopNav(){
navigationBarHidden = false
titleName = "店铺分类管理"
}
func initBottomNav(){
self.view.addSubview(self.bottomNavView)
let createBtn = UIButton(type: .custom)
createBtn.frame = CGRect(x: 0, y: 0, width: KSreenWidth, height: KTabBar_UIHeight)
createBtn.setTitle("创建店铺分类", for: .normal)
createBtn.setImage(UIImage(named: "DS_WB_CJDP"), for: .normal)
createBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)
createBtn.setTitleColor(HexColor("#FD862E"), for: .normal)
createBtn.titleLabel?.font = Font_15
createBtn.addTarget(self, action: #selector(createBtnClick), for: .touchUpInside)
self.bottomNavView.addSubview(createBtn)
}
// MARK: - load data
func loadData(){
showLoading(message: "")
CNLiveShopClassilyViewModel.getCateListWithShopId(shopId: DSUserInfoManager.shared.dsUserInfo.id!) { result, status in
hiddenLoading()
if status == .success{
let model = result as! CNLiveShopClassifyListModel
self.listArray = Array(model.list)
self.headerView.classifyModel = model
self.tableView.reloadData()
}else{
showMessage(message: "获取分类失败")
}
}
}
// MARK: - UITableViewDelegate,UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return self.listArray?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.listArray != nil{
let model = self.listArray![section]
if model.showChild{
return model.child.count
}
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassilyManageTableViewCellID, for: indexPath) as? CNLiveShopClassilyManageTableViewCell
else {
let cell = CNLiveShopClassilyManageTableViewCell()
return cell
}
let model = self.listArray![indexPath.section]
cell.classifyModel = model.child[indexPath.row]
cell.editBtnBlock = { [weak self] classifyModel in
self!.editBtnActionWithMModel(classifyModel: classifyModel, type: .twoLevelClassify)
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: kCNLiveShopClassifySectionHeaderViewID) as! CNLiveShopClassifySectionHeaderView
sectionHeaderView.classifyModel = self.listArray![section]
sectionHeaderView.section = section
sectionHeaderView.showChildCalssifyBlock = { [weak self] section,classifyModel in
self?.showChildCalssifyAction(section: section, classifyModel: classifyModel)
}
sectionHeaderView.addSubClassifyBtnBlock = { [weak self] classifyModel in
self?.addSubClassifyBtnActionWithModel(classifyModel: classifyModel)
}
sectionHeaderView.editBtnBlock = { [weak self] classifyModel in
self?.editBtnActionWithMModel(classifyModel: classifyModel, type: .oneLevelClassify)
}
return sectionHeaderView
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
// MARK: - response method
@objc func createBtnClick(){
let vc = CNLiveCreateShopClassifyViewController()
vc.classifyType = .oneLevelClassify
vc.successCompleteBlock = {[weak self] in
self!.loadData()
}
currentViewController()?.navigationController?.pushViewController(vc, animated: pushAnimated)
}
///显示/隐藏子分类
func showChildCalssifyAction(section:Int,classifyModel:CNLiveShopClassifyModel){
if classifyModel.child.count == 0{
showMessage(message: "当前分类没有子分类")
return
}
classifyModel.showChild = !classifyModel.showChild
self.tableView.reloadSections([section], with: .fade)
}
///添加子分类
func addSubClassifyBtnActionWithModel(classifyModel:CNLiveShopClassifyModel){
let vc = CNLiveCreateShopClassifyViewController()
vc.classifyType = .twoLevelClassify
vc.superClassifyId = classifyModel.id!
vc.successCompleteBlock = {[weak self] in
self!.loadData()
}
currentViewController()?.navigationController?.pushViewController(vc, animated: pushAnimated)
}
func editBtnActionWithMModel(classifyModel:CNLiveShopClassifyModel,type:CNLiveCreateType){
let data = modelToDictionary(classifyModel)
///重新拷贝一个对象
var copyClassifyModel = newDictionaryToModel(data, CNLiveShopClassifyModel.self) as! CNLiveShopClassifyModel
let vc = CNLiveCreateShopClassifyViewController(model: copyClassifyModel, isEdite: true)
vc.classifyType = type
vc.successCompleteBlock = {[weak self] in
self!.loadData()
}
currentViewController()?.navigationController?.pushViewController(vc, animated: true)
}
}