CNLiveHomePageDuanJuCell.swift 4.28 KB
//
//  CNLiveHomePageDuanJuCell.swift
//  metaCode
//
//  Created by zx on 2024/1/8.
//  首页-在追短剧cell

import Foundation
import QMUIKit

typealias DuanJUCellSelectedCallBackBlock = (_ cell: CNLiveHomePageDuanJuCell) -> Void

class CNLiveHomePageDuanJuCell:UITableViewCell,UITableViewDelegate,UITableViewDataSource{
    let kCNLiveAlbumCellIdentifier = "kCNLiveAlbumCellIdentifier"

    var DuanJUCellSelectedCallBack:DuanJUCellSelectedCallBackBlock?
    lazy var modelArr: NSMutableArray = {
        var dataArray = NSMutableArray()
        return dataArray
    }()
    func setModelArr(modelArr:NSMutableArray){
        self.modelArr = NSMutableArray(array: modelArr)
        
        var numCell = 0
        if self.modelArr.count > 4{
            numCell = 4
        }else{
            numCell = self.modelArr.count
        }
        self.tableView.height = CGFloat(numCell)*(45+15*2)
        self.totalView.height = CGFloat(numCell)*(45+15*2) + 55 + 12
        self.tableView.reloadData()
    }
    //MARK: - 懒加载
    ///区块标题
    lazy var blockTitleLabl: UILabel = {
        let titleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: DownSlideRecommendLeftGap+5, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
        titleLbl.text = "在追短剧"
        titleLbl.font = BoldFont_21
        titleLbl.textColor = HexColor("#333333")
        return titleLbl
    }()
    lazy var totalView : UIView = {
        let totalView = UIView(frame: CGRect(x: 15, y: 15, width: KSreenWidth-30, height: 100))
        return totalView
    }()

    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: self.blockTitleLabl.bottom+5, width: KSreenWidth-30, height: 50), style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView.init()
        tableView.backgroundColor = .clear
        tableView.register(CNLiveAlbumCell.self, forCellReuseIdentifier: kCNLiveAlbumCellIdentifier)
        return tableView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none

        self.contentView.addSubview(self.totalView)
       
        // 设置阴影效果
        self.totalView.layer.masksToBounds = false
        //定义view的阴影颜色
        self.totalView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
        //定义view的阴影透明度,注意:如果view没有设置背景色阴影也是不会显示的
        self.totalView.layer.shadowOpacity = 1
        //定义view的阴影宽度,模糊计算的半径
        self.totalView.layer.shadowRadius = 10.0
        //阴影偏移量
        self.totalView.layer.shadowOffset = CGSize(width: 0, height: 0)

        self.totalView.layer.cornerRadius = 12
        self.totalView.backgroundColor = .white
        
        self.totalView.addSubview(self.blockTitleLabl)
        self.totalView.addSubview(self.tableView)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.backgroundColor = HexColor(kCNDownSlideBackgroundColorHex)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.modelArr.count > 4{
            return 4
        }else{
            return self.modelArr.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.modelArr[indexPath.row] as! CNLiveAlbumMsgModel
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveAlbumCellIdentifier) as? CNLiveAlbumCell
        else {
            let cell = CNLiveAlbumCell()
            return cell
        }
        cell.isDetail = false
        cell.albumMsgModel = model
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.DuanJUCellSelectedCallBack!(self)
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 45+15*2
    }
}