CNLiveDownSlideRecommendCell.swift 3.43 KB
//
//  CNLiveDownSlideRecommendCell.swift
//  metaCode
//
//  Created by zx on 2023/10/20.
//  type:4 推荐课程 推荐活动

import Foundation

///推荐课程左间距
let DownSlideRecommendLeftGap: CGFloat = 15
///推荐课程宽高
let DownSlideRecommendWH: CGFloat = (KSreenWidth-DownSlideRecommendLeftGap*3)*0.5


class CNLiveDownSlideRecommendCell: UITableViewCell {

    //slideModel
    var _slideModel : CNLiveDownSlideModel?
    var slideModel : CNLiveDownSlideModel? {
        get{
            _slideModel
        }
        set{
            _slideModel = newValue!
            setupNewUI(model: _slideModel!)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        contentView.backgroundColor =  HexColor(kCNDownSlideBackgroundColorHex)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupNewUI(model:CNLiveDownSlideModel){
        //总块数
        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
        for i in 0..<totalNum{//i是第几行
            if lieIndex == 0{
                //显示左边的块
                let contentMdl = model.contents[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, CGFloat(hangIndex-1)*DownSlideRecommendWH+DownSlideRecommendLeftGap*CGFloat(hangIndex-1), CGFloat(DownSlideRecommendWH), CGFloat(DownSlideRecommendWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster!))
                imageView.tag = i
                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(pushWebAction)))
                contentView.addSubview(imageView)
                lieIndex = 1
            }else{
                //lieIndex==1显示右边的块
                let contentMdl = model.contents[(hangIndex-1)*2+(lieIndex)]
                let imageView = UIImageView(frame: CGRectMake(CGFloat(DownSlideRecommendLeftGap*2)+CGFloat(DownSlideRecommendWH), CGFloat(hangIndex-1)*DownSlideRecommendWH+DownSlideRecommendLeftGap*CGFloat(hangIndex-1), CGFloat(DownSlideRecommendWH), CGFloat(DownSlideRecommendWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster!))
                imageView.tag = i
                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(pushWebAction)))
                contentView.addSubview(imageView)
                lieIndex = 0
                hangIndex += 1
            }
        }
    }
    
    @objc func pushWebAction(ges: UITapGestureRecognizer){
        let imageView = ges.view as! UIImageView
        let i = imageView.tag
        let contentMdl = (self.slideModel?.contents[i])!
        CNLiveDownSlideTool.pushWebVCWithContentsMdl(contentMdl: contentMdl)
    }
    
}