CNLiveHomeTwoShopCell.swift 8.6 KB
//
//  CNLiveHomeTwoShopCell.swift
//  metaCode
//
//  Created by zx on 2025/3/6.
//  type29官方周边cell

import Foundation

class CNLiveHomeTwoShopCell: UITableViewCell {

    //slideModel
    var _slideModel : CNLiveDownSlideModel?
    var slideModel : CNLiveDownSlideModel? {
        get{
            _slideModel
        }
        set{
            _slideModel = newValue!
            for obj in contentView.subviews{
                if obj.tag > 100{
                    obj.removeFromSuperview()
                }
            }
            setupNewUI(model: _slideModel!)
        }
    }
    //MARK: - 懒加载
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: 0, width: KSreenWidth-15-50, height: 25))
        titleLbl.font = BoldFont_18
        titleLbl.textColor = HexColor("#000102")
        return titleLbl
    }()
    //more
    lazy var detailBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: KSreenWidth-50, y: self.titleLabel.top, 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
        contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        contentView.clipsToBounds = true
        contentView.addSubview(self.titleLabel)
        contentView.addSubview(self.detailBtn)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupNewUI(model:CNLiveDownSlideModel){
        self.titleLabel.text = model.blockName
        var titleBottom:CGFloat = 40.0
        if model.showType == "1"{
            //不展示标题 和按钮
            titleBottom = 0.0
            self.detailBtn.isHidden = true
            self.titleLabel.isHidden = true
        }else{
            //展示标题
            self.titleLabel.isHidden = false
            if (model.moreUrl.count > 0) && (model.moreType.count > 0){
                //展示更多按钮
                self.detailBtn.isHidden = false
                titleBottom = 40.0
            }else{
                //不展示更多按钮
                self.detailBtn.isHidden = true
                titleBottom = 40.0
            }
        }
                
        //总块数
        let contentsNum = model.contents.count
        //model行数
        let row = model.rowNum
        //总显示块数/循环数
        var totalNum = (contentsNum >= row*2) ? row*2 : contentsNum
        if row == 0{//row为0 , contents全部显示
            totalNum = contentsNum
        }
        //lieIndex 当前是contents的第0列还是第1列
        var lieIndex = 0
        //hangIndex 当前是第几行,1...n
        var hangIndex = 1
        
        let imgWH = (KSreenWidth-15*3)*0.5
        let bottomH = 55.0
        let rowH = imgWH+bottomH+15.0

        for i in 0..<totalNum{//i是第几行
            if lieIndex == 0{
                //显示左边的块
                let contentMdl = model.contents[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, titleBottom+CGFloat(hangIndex-1)*rowH, CGFloat(imgWH), CGFloat(imgWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster))
                imageView.tag = i+1000
                imageView.layer.cornerRadius = 9
                imageView.clipsToBounds = true
                imageView.contentMode = .scaleAspectFill

                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(imgTapAction)))
                contentView.addSubview(imageView)
                
                var titleStr = contentMdl.title
                var subTitleStr = "¥"
                if contentMdl.number.count > 0{
                    if let numberMdl = jsonToModel(contentMdl.number, CNLiveDownSlideContentsNumerModel.self) as? CNLiveDownSlideContentsNumerModel{
                        subTitleStr = "¥"+numberMdl.price
                    }
                }
                
                let titleLbl = UILabel(frame: CGRect(x: imageView.left, y: imageView.bottom+10, width: imageView.width, height: 21))
                titleLbl.font = BoldFont_15
                titleLbl.textColor = HexColor("#333333")
                titleLbl.text = titleStr
                titleLbl.tag = i+1000
                contentView.addSubview(titleLbl)

                let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y: titleLbl.bottom, width: titleLbl.width, height: 24))
                subTitleLbl.font = Font_17
                subTitleLbl.textColor = HexColor("#E56E32")
                subTitleLbl.text = subTitleStr
                subTitleLbl.tag = i+1000
                contentView.addSubview(subTitleLbl)

                lieIndex = 1
            }else{
                //lieIndex==1显示右边的块
                let contentMdl = model.contents[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(CGFloat(DownSlideRecommendLeftGap*2)+CGFloat(imgWH), CGFloat(titleBottom)+CGFloat(hangIndex-1)*rowH, CGFloat(imgWH), CGFloat(imgWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster))
                imageView.tag = i+1000
                imageView.layer.cornerRadius = 14
                imageView.clipsToBounds = true
                imageView.contentMode = .scaleAspectFill

                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(imgTapAction)))
                contentView.addSubview(imageView)
                
                var titleStr = contentMdl.title
                var subTitleStr = "¥"
                if contentMdl.number.count > 0{
                    if let numberMdl = jsonToModel(contentMdl.number, CNLiveDownSlideContentsNumerModel.self) as? CNLiveDownSlideContentsNumerModel{
                        subTitleStr = "¥"+numberMdl.price
                    }
                }
                
                let titleLbl = UILabel(frame: CGRect(x: imageView.left, y: imageView.bottom+10, width: imageView.width, height: 21))
                titleLbl.font = BoldFont_15
                titleLbl.textColor = HexColor("#333333")
                titleLbl.text = titleStr
                titleLbl.tag = i+1000
                contentView.addSubview(titleLbl)

                let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y: titleLbl.bottom, width: titleLbl.width, height: 24))
                subTitleLbl.font = Font_17
                subTitleLbl.textColor = HexColor("#E56E32")
                subTitleLbl.tag = i+1000
                subTitleLbl.text = subTitleStr
                contentView.addSubview(subTitleLbl)
                
                lieIndex = 0
                hangIndex += 1
            }
        }
    }
    
    @objc func detailBtnAction(){
        if self.slideModel?.moreType == "1"{
            //原生
            let vc = CNLiveHomeMoreController()
            vc.slideModel = self.slideModel
            currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }else if self.slideModel?.moreType == "3"{
            //webView moreurl
            CNLiveDownSlideTool.pushWebVCWithContentsMdl(model: "3", contentId: "", pid: "", shareUrl: "", thirdUrl: self.slideModel?.moreUrl ?? "", title: "", s_to: "", trailerId: "", controller: nil, slideModel: nil)
        }else{
            
        }
    }
    
    @objc func imgTapAction(ges: UITapGestureRecognizer){
        let imageView = ges.view as! UIImageView
        let i = imageView.tag-1000
        let contentMdl = (self.slideModel?.contents[i])!
        let urlString = "https://managemall.cnlive.com/html/mall/1/#/pages/main/detail/detail?"+"id=\(contentMdl.contentId)&uid=\(UserInfoManager.shared.userInfo.uid)"
        
        CNLiveDownSlideTool.pushWebAndVodVCWithContentsMdl(model: contentMdl.model , contentId: contentMdl.contentId , pid: contentMdl.pid , shareUrl: contentMdl.shareUrl , thirdUrl: urlString , title: contentMdl.title , s_to: contentMdl.s_to ,trailerId: contentMdl.trailerId , controller: nil, slideModel: nil)
    }
    
}