CNLiveVodShopBottomView.swift 6.1 KB
//
//  CNLiveVodShopBottomView.swift
//  metaCode
//
//  Created by zx on 2025/3/21.
//

import Foundation

typealias CNLiveVodTrailersExpandCellHiddenBlock = ()->Void

class CNLiveVodShopBottomView:UIView, UITableViewDataSource, UITableViewDelegate{
    var expandHideBlock:CNLiveVodTrailersExpandCellHiddenBlock?//收起
    
    var blockTitle:String = "相关推荐"
    var _vodPlayShopModels : [CNLiveVodPlayShopModel]?
    var vodPlayShopModels : [CNLiveVodPlayShopModel]? {
        get{
            _vodPlayShopModels
        }
        set{
            _vodPlayShopModels = newValue!
            self.setupNewUI(model: _vodPlayShopModels!)
        }
    }
    
    //默认电商商品list
    var _albumModels : [CNLiveAlbumModel]?
    var albumModels : [CNLiveAlbumModel]? {
        get{
            _albumModels
        }
        set{
            _albumModels = newValue!
            self.setupNewUI(goodsModels: _albumModels!)
        }
    }

    
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = BoldFont_18
        titleLbl.textColor = HexColor("#000102")
        titleLbl.text = "相关推荐"
        return titleLbl
    }()
    //more
    lazy var detailBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 24)
        expandBtn.setImage(UIImage(named: "home_player_expand"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_expand"), for: .highlighted)
        expandBtn.addTarget(self, action: #selector(detailBtnAction), for: .touchUpInside)
        return expandBtn
    }()
    let kCNLiveVodShopExpandCellIdentifier = "kCNLiveVodShopExpandCellIdentifier"

    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(CNLiveVodShopExpandCell.self, forCellReuseIdentifier: kCNLiveVodShopExpandCellIdentifier)
        tableView.backgroundColor = .white
        return tableView
    }()
    ///数据源
    lazy var dataArray: NSMutableArray = {
        var dataArray = NSMutableArray()
        return dataArray
    }()

    // MARK: - init
    init() {
        super.init(frame: CGRectZero)
        self.initializeThings()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initializeThings()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initializeThings(){
        self.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(15)
            make.width.lessThanOrEqualToSuperview().offset(-80)
        }
        
        self.addSubview(self.detailBtn)
        detailBtn.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.top)
            make.width.equalTo(50)
            make.right.equalToSuperview()
            make.height.equalTo(24)
        }
        
        self.addSubview(tableView)
        tableView.frame = CGRect(x: 0, y: 55, width: KSreenWidth, height: self.height-55)
        tableView.backgroundColor = .white
    }
    
    func setupNewUI(model:[CNLiveVodPlayShopModel]){
        self.titleLabel.text = blockTitle
        self.dataArray = NSMutableArray()//NSMutableArray(array: model)
        self.dataArray.add(model)
        self.tableView.reloadData()
    }
    func setupNewUI(goodsModels:[CNLiveAlbumModel]){
        self.titleLabel.text = blockTitle
        self.dataArray = NSMutableArray()//NSMutableArray(array: model)
        self.dataArray.add(goodsModels)
        self.tableView.reloadData()
    }


    // MARK: - UITableViewDelegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let vodPlayShopModel = self.dataArray[indexPath.row] as? [CNLiveVodPlayShopModel]{
            //商品
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveVodShopExpandCellIdentifier) as? CNLiveVodShopExpandCell
            else {
                let cell = CNLiveVodShopExpandCell()
                return cell
            }

            cell.vodPlayShopModel = vodPlayShopModel
            return cell
        }else if let vodPlayShopModel = self.dataArray[indexPath.row] as? [CNLiveAlbumModel]{
            //默认商品
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveVodShopExpandCellIdentifier) as? CNLiveVodShopExpandCell
            else {
                let cell = CNLiveVodShopExpandCell()
                return cell
            }

            cell.albumModels = vodPlayShopModel
            return cell
        }
        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if let vodPlayShopModel = self.dataArray[indexPath.row] as? [CNLiveVodPlayShopModel]{
            let imgWH:Float = Float((KSreenWidth-15*3)*0.5)
            let bottomH:Float = 55.0
            let rowH:Float = imgWH+bottomH+15.0
            let hang = Int(ceil(CGFloat(vodPlayShopModel.count)*0.5))
            let totalRowH = Float(Float(hang)*rowH)
            return CGFloat(totalRowH)
        }else if let vodPlayShopModel = self.dataArray[indexPath.row] as? [CNLiveAlbumModel]{
            let imgWH:Float = Float((KSreenWidth-15*3)*0.5)
            let bottomH:Float = 55.0
            let rowH:Float = imgWH+bottomH+15.0
            let hang = Int(ceil(CGFloat(vodPlayShopModel.count)*0.5))
            let totalRowH = Float(Float(hang)*rowH)
            return CGFloat(totalRowH)
        }
        return 60
    }

    // MARK: - 收起
    @objc func detailBtnAction(){
        self.expandHideBlock?()
    }
}