Swift UICollectionViewCell 基础进阶用法

290 阅读1分钟

针对Swift UICollectionViewCell的用法解答

步骤: 1.设置frame布局 2.设置Model的传值

  1. UICollectionViewCell 的高级自定义设置
  2. CollectionView 的UI 布局设置和运用

备注: 1.addOtherCorner 这个的用法,请关注,后续会补上高度自定义方法设置圆角方法 2.YJFontRegular(12) font 的类库封装用法,请关注,后续会补上高度自定义方法设置圆角方法

import Foundation

class ShopCell: UICollectionViewCell {
 
 /// 标题
 lazy var titleLB:UILabel = {
     let lb = UILabel(frame: CGRect(x:(self.contentView.width - 100)/2, y:  0 , width:100 , height: 25))
     lb.font = YJFontRegular(12)
     lb.textColor = .white
     lb.text = "Games"
     lb.textAlignment = .center
     lb.backgroundColor = .rgb(160, 192, 214)
     lb.addOtherCorner([.bottomLeft,.bottomRight], radius: 4)
     return lb
 }()
 
 
 /// 道具图片
 lazy var propImgView: UIImageView = {
     let iv = UIImageView(frame: CGRect(x:(self.contentView.width - 60)/2, y: titleLB.bottom + 30 , width:60 , height: 60))
     iv.contentMode = .scaleAspectFill
     iv.image = UIImage(named: "yuli_icon_hp")
     return iv
 }()
 
 /// 道具标题
 lazy var propTitleLB:UILabel = {
     let lb = UILabel(frame: CGRect(x:(self.contentView.width - 100)/2, y:  propImgView.bottom + 10 , width:100 , height: 25))
     lb.font = YJFontRegular(12)
     lb.textColor = .black
     lb.text = "Games Chip LV.1"
     lb.textAlignment = .center
     return lb
 }()
 
 /// buy View
 lazy var bgBottomView : UIView = {
     let v = UIView(frame: CGRect(x: 0, y:self.contentView.height - 40, width:self.contentView.width , height: 40 ))
         v.backgroundColor = .rgb(229, 250, 252)
     return v
     
 }()
 
 
 /// 价格
 lazy var priceLB:UILabel = {
     let lb = UILabel(frame: CGRect(x:5 , y: 0 , width:self.contentView.width - 10 - 60 - 10 , height: 40))
     lb.font = YJFontMedium(14)
     lb.textColor = .rgba(223, 116, 86, 1)
     lb.textAlignment = .center
     lb.text = "2000 ARG"
     return lb
     
 }()
 
 
 /// buy
 lazy var priceBtn:UIButton = {

     let btn = UIButton(frame: CGRect(x:self.contentView.width - 10 - 60, y:  5, width: 60, height: 30))
     btn.setTitle("BUY", for: UIControl.State.normal)
     btn.titleLabel?.font = YJFontSemibold(16)
     btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
     btn.backgroundColor = .rgba(223, 116, 86, 1)
     btn.layer.cornerRadius = 15
     return btn
 }()

 
 
 required init?(coder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
 }
 
 override init(frame: CGRect) {
     super.init(frame: frame)
     layer.masksToBounds = true
     layer.cornerRadius = 5
     backgroundColor = .white
 }
 
 override func layoutSubviews() {
     super.layoutSubviews()
     layoutsubview()
     
 }
 
/// 传值显示和交互Model
 open var model = UserModel() {
     didSet{
         
     }
 }
}


/// MARK:  UI布局

extension ShopCell {

 func layoutsubview() {
     
     self.contentView.addSubview(titleLB)
     self.contentView.addSubview(propImgView)
     self.contentView.addSubview(propTitleLB)
     self.contentView.addSubview(bgBottomView)
     bgBottomView.addSubview(priceLB)
     bgBottomView.addSubview(priceBtn)

 }
 
}