123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- //
- // YMTodayRecomCell.swift
- // MvpGoods
- //
- // Created by 小花 on 2019/4/11.
- // Copyright © 2019 MVP. All rights reserved.
- //
- import UIKit
- protocol YMTodayRecomCellDelegate: NSObjectProtocol {
-
- func didSelectedItem(index: Int,model: YMMainGoodModel)
- }
- class YMTodayRecomCell: UICollectionViewCell {
-
- weak var delegate: YMTodayRecomCellDelegate!
-
- var cellID = "cellId"
-
- var collection: UICollectionView?
- var dataArr: [YMMainGoodModel]?
-
- required init?(coder aDecoder: NSCoder) {
- self.init()
- }
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- configSubViews()
- }
-
- func configSubViews() {
-
- let bg = UIImageView(frame: CGRect(x: 5, y: 5, width: SCREENW-10, height: fitSize(x: 129)))
- bg.backgroundColor = UIColor.gloadRedColor()
- bg.isUserInteractionEnabled = true
- bg.layer.cornerRadius = 5;
- self.addSubview(bg)
-
- let titleLb = UILabel(frame: CGRect(x: 19, y: fitSize(x: 7), width: 100, height: fitSize(x: 24)))
- titleLb.textColor = UIColor.white
- titleLb.font = UIFont.systemFont(ofSize: fitSize(x: 17))
- titleLb.text = "猜你喜欢"
- bg.addSubview(titleLb)
-
- let layout = UICollectionViewFlowLayout()
- layout.scrollDirection = .horizontal
- layout.itemSize = CGSize(width: fitSize(x: 118), height: fitSize(x: 187))
- layout.minimumLineSpacing = 10
- layout.minimumInteritemSpacing = 0
-
- let collectionView = UICollectionView(frame: CGRect(x: 0, y: fitSize(x: 38), width: self.width, height: fitSize(x: 190)), collectionViewLayout:layout)
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.showsHorizontalScrollIndicator = false
- collectionView.backgroundColor = UIColor.clear
- collectionView.register(YMGroupChildCollectionCell.self, forCellWithReuseIdentifier: cellID)
- self.addSubview(collectionView)
- self.collection = collectionView
- }
-
- public func setDataArray(dataArr: [YMMainGoodModel]) {
- self.dataArr = dataArr
- self.collection?.reloadData()
- }
-
- }
- extension YMTodayRecomCell: UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return self.dataArr?.count ?? 0
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! YMGroupChildCollectionCell
- cell.model = self.dataArr?[indexPath.row]
- cell.imgView.layer.cornerRadius = fitSize(x: 118/2)
- cell.imgView.layer.masksToBounds = true
- cell.titleLabel.numberOfLines = 2
- cell.titleLabel.font = UIFont.systemFont(ofSize: 12)
- return cell
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
- return UIEdgeInsetsMake(0, 10, 0, 20)
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- let model = self.dataArr?[indexPath.row]
- if (self.delegate != nil) {
- self.delegate?.didSelectedItem(index: indexPath.row, model: model!)
- }
- }
-
- }
|