CNLiveVodAlbumListCell.swift 3.75 KB
//
//  CNLiveVodAlbumListCell.swift
//  metaCode
//
//  Created by zx on 2025/3/4.
//

import Foundation

typealias CNLiveVodAlbumListCellSelectBlock = (_ vodModel:CNLiveVodAlbumListModel,_ index:Int,_ albumListModels:NSMutableArray,_ vodAlbumListBaseModel:CNLiveVodAlbumListBaseModel?)->Void;

class CNLiveVodAlbumListCell:UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource{
    var currSeleIdx:Int = 0
    var selectBlock:CNLiveVodAlbumListCellSelectBlock?
    //实时热榜的列表
    lazy var albumListArr: NSMutableArray = {
        var albumListArr = NSMutableArray()
        return albumListArr
    }()
    let kCNLiveVodAlbumListCollectionCellIdentifier = "kCNLiveVodAlbumListCollectionCellIdentifier"

    lazy var realHotCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSizeMake(50, 50)
        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(CNLiveVodAlbumListCollectionCell.self, forCellWithReuseIdentifier: kCNLiveVodAlbumListCollectionCellIdentifier)
        return cv
    }()
    
    //
    var _vodAlbumListBaseModel : CNLiveVodAlbumListBaseModel?
    var vodAlbumListBaseModel : CNLiveVodAlbumListBaseModel? {
        get{
            _vodAlbumListBaseModel
        }
        set{
            _vodAlbumListBaseModel = newValue!
            self.setupNewUI(model: vodAlbumListBaseModel!)
        }
    }
    
    //MARK: - 懒加载

    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.realHotCollectionView)
        self.realHotCollectionView.frame = CGRect(x: 10, y: 0, width: KSreenWidth-20, height: 51)
    }
    
    func setupNewUI(model:CNLiveVodAlbumListBaseModel){
        self.albumListArr = NSMutableArray(array: model.list)
        self.realHotCollectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.albumListArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveVodAlbumListCollectionCellIdentifier, for: indexPath) as! CNLiveVodAlbumListCollectionCell
        if self.currSeleIdx == indexPath.row{
            //当前cell选中
            cell.isJiShuSelected = true
        }else{
            //不选中
            cell.isJiShuSelected = false
        }
        cell.num = indexPath.row
        cell.vodAlbumListModel = self.albumListArr[indexPath.row] as? CNLiveVodAlbumListModel
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vodAlbumListModel = self.albumListArr[indexPath.row] as? CNLiveVodAlbumListModel{
            //点击某集,刷新页面
            self.selectBlock?(vodAlbumListModel, indexPath.row, self.albumListArr, self.vodAlbumListBaseModel)
            self.currSeleIdx = indexPath.row
            self.realHotCollectionView.reloadData()
            
        }
    }

}