CNLiveShopClassifySwitchTableViewCell.swift 2.39 KB
//
//  CNLiveShopClassifySwitchTableViewCell.swift
//  metaCode
//
//  Created by zx on 2023/11/18.
//

import Foundation

///排序类型
enum CNLiveShopClassifySwitchType:Int {
    ///是否显示分类
    case classify
    ///是否显示导航栏
    case nav
}

typealias switchBlock = (_ on:Bool,_ type:CNLiveShopClassifySwitchType)->Void

///包含 Switch的 cell
class CNLiveShopClassifySwitchTableViewCell: UITableViewCell {
    var switchType:CNLiveShopClassifySwitchType?
    var switchClickBlock:switchBlock?

    // MARK: - Lazy
    lazy var titlelab: UILabel = {
        let titlelab = UILabel.init()
        titlelab.textColor = HexColor("#333333")
        titlelab.textAlignment = .left
        titlelab.font = Font_14
        titlelab.adjustsFontSizeToFitWidth=true
        return titlelab
    }()
    lazy var star: UILabel = {
        let star = UILabel.init(frame: CGRect(x: 5, y: 0, width: 6, height: 50))
        star.textColor = HexColor("#FF725C")
        star.textAlignment = .left
        star.text = "*"
        star.font = Font_12
        return star
    }()
    lazy var rightSwitch: UISwitch = {
        let rightSwitch = UISwitch.init()
        rightSwitch.tintColor = .green
        rightSwitch.addTarget(self, action: #selector(switchAction), for: .valueChanged)
        return rightSwitch
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 初始化UI
    func initUI(){
        self.contentView.addSubview(self.titlelab)
        self.contentView.addSubview(self.star)
        self.contentView.addSubview(self.rightSwitch)
        self.p_massony()
    }
    
    func p_massony(){
        
        self.titlelab.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.centerY.equalTo(self.contentView.centerY)
        }
        self.rightSwitch.snp.makeConstraints { make in
            
            make.right.equalTo(-15)
            make.centerY.equalTo(self.contentView.centerY)
            make.size.equalTo(CGSize(width: 45, height: 22))
        }
    }
    // MARK: - switchAction
    @objc func switchAction(sender:UISwitch){
        self.switchClickBlock!(self.rightSwitch.isOn, self.switchType!)
    }
}