CNLiveVodTrailersCell.swift 5.44 KB
//
//  CNLiveVodTrailersCell.swift
//  metaCode
//
//  Created by zx on 2025/3/4.
//  点播-看点

import Foundation

typealias CNLiveVodTrailersCellExpandBlock = (_ model:CNLiveVodPlayBlockListModel)->Void
typealias CNLiveVodTrailersCellSelectBlock = (_ model:CNLiveVodPlayBlockListListModel,_ index:Int ,_ albumListArr: NSMutableArray)->Void

class CNLiveVodTrailersCell:UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource{
    
    var currSeleIdx:Int = -1
    var expandBlock:CNLiveVodTrailersCellExpandBlock?
    var selectBlock:CNLiveVodTrailersCellSelectBlock?
    lazy var albumListArr: NSMutableArray = {
        var albumListArr = NSMutableArray()
        return albumListArr
    }()
    let kCNLiveVodTrailersCollectionCellIdentifier = "kCNLiveVodTrailersCollectionCellIdentifier"

    lazy var realHotCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSizeMake(165, 93+76)
        layout.sectionInset = .init(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumLineSpacing = 5
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.delegate = self
        cv.dataSource = self
        cv.backgroundColor = .white
        cv.showsHorizontalScrollIndicator = false
        cv.register(CNLiveVodTrailersCollectionCell.self, forCellWithReuseIdentifier: kCNLiveVodTrailersCollectionCellIdentifier)
        return cv
    }()

    
    var _vodPlayBlockListModel : CNLiveVodPlayBlockListModel?
    var vodPlayBlockListModel : CNLiveVodPlayBlockListModel? {
        get{
            _vodPlayBlockListModel
        }
        set{
            _vodPlayBlockListModel = newValue!
            self.setupNewUI(model: vodPlayBlockListModel!)
        }
    }
    
    //MARK: - 懒加载
    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_more"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_more"), for: .highlighted)
        expandBtn.addTarget(self, action: #selector(detailBtnAction), for: .touchUpInside)
        return expandBtn
    }()


    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    private func setupUI() {
        contentView.backgroundColor = .white
        self.contentView.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview()
            make.width.lessThanOrEqualToSuperview().offset(-80)
        }
        
        self.contentView.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.contentView.addSubview(self.realHotCollectionView)
        self.realHotCollectionView.frame = CGRect(x: 10, y: 39, width: KSreenWidth-20, height: 93+76)

        self.contentView.clipsToBounds = true
    }
    
    func setupNewUI(model:CNLiveVodPlayBlockListModel){
        self.titleLabel.text = model.name
        self.albumListArr = NSMutableArray(array: model.list)
        var index = self.currSeleIdx >= 0 ? self.currSeleIdx : 0
        self.realHotCollectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .left, animated: false)
        self.realHotCollectionView.reloadData()
        
    }

    @objc func detailBtnAction(){
        self.expandBlock?(self.vodPlayBlockListModel!)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.albumListArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveVodTrailersCollectionCellIdentifier, for: indexPath) as! CNLiveVodTrailersCollectionCell
        cell.num = indexPath.row
        //获取当前选中集数
        if indexPath.row == self.currSeleIdx{
            cell.isJiShuSelected = true
        }else{
            cell.isJiShuSelected = false
        }
        cell.vodlistModel = self.albumListArr[indexPath.row] as? CNLiveVodPlayBlockListListModel
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vodAlbumListModel = self.albumListArr[indexPath.row] as? CNLiveVodPlayBlockListListModel{
            print("1111111看点index:\(indexPath.row)")
            self.currSeleIdx = indexPath.row
            //点击某集,刷新页面
            self.selectBlock?(vodAlbumListModel,currSeleIdx,self.albumListArr)
            self.realHotCollectionView.reloadData()
        }
    }

    
}