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
//
// SelectContentView.swift
// GeliBusinessPlatform
//
// Created by 刘俊宏 on 2020/4/22.
// Copyright © 2020 junye lu. All rights reserved.
//
import Foundation
protocol SelectContentViewDelegate {
func didselectCell(sender:Int)
func selectContentvRemoveForSup()
}
class SelectContentView: UIView, UITableViewDelegate, UITableViewDataSource {
var delegate:SelectContentViewDelegate?
@IBOutlet weak var tabContentHeight: NSLayoutConstraint!
@IBOutlet weak var tabContentV: UIView!
var contentView:UIView!
var tableV :UITableView!
var selectIndx : Int = 0 {
didSet{
tableV.selectRow(at: IndexPath(row: selectIndx, section: 0), animated: false, scrollPosition: .none)
}
}
var dataArr : [String] = []{
didSet{
if dataArr.count > 3 {
if dataArr.count > 8 {
tabContentHeight.constant = 50 * 7 * glscale + 5
}else{
tabContentHeight.constant = CGFloat((50 * Int(glscale)) * dataArr.count + 5)
}
}else{
tabContentHeight.constant = 150 * glscale + 5
}
tableV.reloadData()
}
}
func setTabv() {
tableV = UITableView()
tabContentV.addSubview(tableV);
tableV.snp.makeConstraints { (make) in
make.left.right.bottom.equalToSuperview()
make.top.equalTo(5)
}
tableV.delegate = self
tableV.dataSource = self
tableV.separatorStyle = .none
tableV?.register(UINib(nibName: "TitleContentCell", bundle: nil), forCellReuseIdentifier: "TitleContentCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TitleContentCell") as! TitleContentCell
let titleStr = dataArr[indexPath.row]
cell.contentLbl.text = ""
cell.titleLbl.text = titleStr
if selectIndx == indexPath.row {
cell.titleLbl.textColor = UIColor.init(named: "蓝色字体颜色")
}else{
cell.titleLbl.textColor = UIColor.init(named: "标题字颜色")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate?.didselectCell(sender: indexPath.row)
self.removeFromSuperview()
}
@IBAction func balckTapAction(_ sender: UITapGestureRecognizer) {
self.delegate?.selectContentvRemoveForSup()
self.removeFromSuperview()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50*glscale
}
//初始化时将xib中的view添加进来
override init(frame: CGRect) {
super.init(frame: frame)
contentView = loadViewFromNib()
addSubview(contentView)
contentView.snp.makeConstraints { (make) in
make.left.top.right.bottom.equalToSuperview()
}
setTabv()
}
// /初始化时将xib中的view添加进来
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
contentView = loadViewFromNib()
addSubview(contentView)
contentView.snp.makeConstraints { (make) in
make.left.top.right.bottom.equalToSuperview()
}
setTabv()
}
//加载xib
func loadViewFromNib() -> UIView {
let className = type(of: self)
let bundle = Bundle(for: className)
let name = NSStringFromClass(className).components(separatedBy: ".").last
let nib = UINib(nibName: name!, bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
}