CNLiveVodShopExpandCell.swift 10.4 KB
//
//  CNLiveVodShopExpandCell.swift
//  metaCode
//
//  Created by zx on 2025/3/21.
//

import Foundation

class CNLiveVodShopExpandCell: UITableViewCell {
    //
    var _vodPlayShopModel : [CNLiveVodPlayShopModel]?
    var vodPlayShopModel : [CNLiveVodPlayShopModel]? {
        get{
            _vodPlayShopModel
        }
        set{
            _vodPlayShopModel = newValue!
            for obj in contentView.subviews{
                if obj.tag > 100{
                    obj.removeFromSuperview()
                }
            }
            setupNewUI(model: _vodPlayShopModel!)
        }
    }
    //默认电商商品list
    var _albumModels : [CNLiveAlbumModel]?
    var albumModels : [CNLiveAlbumModel]? {
        get{
            _albumModels
        }
        set{
            _albumModels = newValue!
            for obj in contentView.subviews{
                if obj.tag > 100{
                    obj.removeFromSuperview()
                }
            }
            self.setupNewUI(goodsModels: _albumModels!)
        }
    }

    
    //MARK: - 懒加载
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        contentView.backgroundColor = .white
        contentView.clipsToBounds = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //默认商品
    func setupNewUI(goodsModels:[CNLiveAlbumModel]){
                
        //总显示块数/循环数
        let totalNum = goodsModels.count
        //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 = goodsModels[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, 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)
                
                let titleStr = contentMdl.title
                let subTitleStr = \(contentMdl.numberModel?.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 = goodsModels[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(CGFloat(DownSlideRecommendLeftGap*2)+CGFloat(imgWH), 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)
               
                let titleStr = contentMdl.title
                let subTitleStr = \(contentMdl.numberModel?.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
            }
        }
    }

    func setupNewUI(model:[CNLiveVodPlayShopModel]){
                
        //总显示块数/循环数
        let totalNum = model.count
        //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[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, CGFloat(hangIndex-1)*rowH, CGFloat(imgWH), CGFloat(imgWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.img))
                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)
                
                let titleStr = contentMdl.title
                let subTitleStr = \(contentMdl.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[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(CGFloat(DownSlideRecommendLeftGap*2)+CGFloat(imgWH), CGFloat(hangIndex-1)*rowH, CGFloat(imgWH), CGFloat(imgWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.img))
                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)
               
                let titleStr = contentMdl.title
                let subTitleStr = \(contentMdl.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 imgTapAction(ges: UITapGestureRecognizer){
        let imageView = ges.view as! UIImageView
        let i = imageView.tag-1000
        if self.vodPlayShopModel?.count ?? 0 > 0{
            let contentMdl = (self.vodPlayShopModel?[i])!
            let urlString = hubilieDSHost+"pages/main/detail/detail?"+"id=\(contentMdl.id)&uid=\(UserInfoManager.shared.userInfo.uid)"
            
            CNLiveDownSlideTool.pushWebAndVodVCWithContentsMdl(model: kCNDownSlideContentsModelModelProduct , contentId: "" , pid: "" , shareUrl: "" , thirdUrl: urlString , title: contentMdl.title , s_to: "" ,trailerId: "" , controller: nil, slideModel: nil)
        }else if self.albumModels?.count ?? 0 > 0{
            let contentMdl = (self.albumModels?[i])!
            let urlString = hubilieDSHost+"pages/main/detail/detail?"+"id=\(contentMdl.contentId)&uid=\(UserInfoManager.shared.userInfo.uid)"
            
            CNLiveDownSlideTool.pushWebAndVodVCWithContentsMdl(model: kCNDownSlideContentsModelModelProduct , contentId: "" , pid: "" , shareUrl: "" , thirdUrl: urlString , title: contentMdl.title , s_to: "" ,trailerId: "" , controller: nil, slideModel: nil)
        }
    }
    
}