最近項目需求有個列表抽屜效果让簿,之前寫過忘了壤短,現(xiàn)在總結(jié)記錄一下江掩,代碼如下:
class HTClassSearchViewController: HTClassBaseViewController {
var var_rightImageView = UIImageView()
var var_stateArray:[String] = ["0","0","0","0"]
var var_sectionArray: [String] = [
"How to start recording screen?",
"Where is the video recording file saved?",
"The screen recording process stops suddenly.",
"What is the reason for no sound?",
]
var var_dataArray: [HTClassSearchModel] = [
HTClassSearchModel(title: "Click the start screen recording button, we have configured the default parameters for you"),
HTClassSearchModel(title: "The recorded video is saved to the APP by default, and you can manage or view the video in the video library; in the parameter setting of the screen recording page, you can adjust the storage location of the recorded video: only in the APP, or both in the APP and the system photo album."),
HTClassSearchModel(title: "The reason is \"null\".This situation is caused by insufficient memory and CPU currently used by the iOS system, and the iOS system will actively close the running applications. It is recommended that you try to close the unused applications in the background before starting to record the screen to ensure the normal recording process."),
HTClassSearchModel(title: "This is an iOS system bug. When you encounter such problems, please click the home button to move the concave main screen, and then return to need to record"),
]
override func viewDidLoad() {
super.viewDidLoad()
ht_SetNav()
self.var_leftBtn.setTitle("Home", for: .normal)
self.view.addSubview(self.searchTableView)
self.searchTableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
lazy var searchTableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.rowHeight = UITableView.automaticDimension
tableView.register(HTClassSearchTableViewCell.self, forCellReuseIdentifier: "HTClassSearchTableViewCell")
tableView.contentInsetAdjustmentBehavior = .never
return tableView
}()
}
extension HTClassSearchViewController: UITableViewDelegate,UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return self.var_sectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if var_stateArray[section] == "1" {
return 1
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HTClassSearchTableViewCell", for: indexPath) as! HTClassSearchTableViewCell
cell.model = self.var_dataArray[indexPath.section]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.var_sectionArray[section]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = UIView(frame: CGRectMake(0, 0, KScreenWidth, 50))
sectionView.backgroundColor = .white
let titleLabel = UILabel(frame: CGRectMake(15, 0, KScreenWidth-80, 50))
titleLabel.text = "\(section+1)" + "." + self.var_sectionArray[section]
titleLabel.textColor = Color_666666
titleLabel.font = SystemFont(14)
titleLabel.textAlignment = .left
sectionView.addSubview(titleLabel)
var_rightImageView = UIImageView(frame: CGRectMake(KScreenWidth-35, (50-10)/2, 10, 10))
var_rightImageView.image = UIImage(named: "Frame 57")
sectionView.addSubview(var_rightImageView)
if self.var_stateArray[section] == "1" {
var_rightImageView.image = UIImage(named: "Frame 58")
}else {
var_rightImageView.image = UIImage(named: "Frame 57")
}
let button = UIButton(type: .custom)
button.frame = CGRectMake(0, 0, KScreenWidth, 50)
button.backgroundColor = .clear
button.tag = section+1
sectionView.addSubview(button)
if section == 0 {
button.addTarget(self, action: #selector(ht_ButtonPress(_:)), for: .touchUpInside)
}else {
button.addTarget(self, action: #selector(ht_ButtonPress2(_:)), for: .touchUpInside)
}
return sectionView
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
extension HTClassSearchViewController {
@objc func ht_ButtonPress(_ sender: UIButton) {
if self.var_stateArray[sender.tag - 1] == "1" {
self.var_stateArray[sender.tag - 1] = "0"
}else {
self.var_stateArray[sender.tag - 1] = "1"
}
self.searchTableView.reloadSections([sender.tag - 1], with: .automatic)
}
@objc func ht_ButtonPress2(_ sender: UIButton) {
if self.var_stateArray[sender.tag - 1] == "1" {
self.var_stateArray[sender.tag - 1] = "0"
}else {
self.var_stateArray[sender.tag - 1] = "1"
}
self.searchTableView.reloadSections([sender.tag - 1], with: .automatic)
}
}
直接搞定