UITableView和UICollectionView

Swift 学习 第2天 - UICollectionViewC

2022-08-11  本文已影响0人  失忆的程序员
UICollectionViewCell

cell 中的实现

//

import UIKit
import SnapKit

class BaseCollectionViewCell: UICollectionViewCell
{
    
    class func initWithCollectionViewCell(_ collectionView: UICollectionView, _ indexPath: NSIndexPath, _ Identifier: String) -> BaseCollectionViewCell
    {
        var cell = BaseCollectionViewCell()
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier, for: indexPath as IndexPath) as! BaseCollectionViewCell
        return cell
    }
    
    // 根据model 填充Cell
    func cellForModel(model: BaseModel?) {
        if let tempModel = model {
            nameLb.text = tempModel.name
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var nameLb : UILabel = {
        let nameLb = UILabel()
        nameLb.backgroundColor = UIColor.orange
        nameLb.text = "哈哈哈"
        nameLb.textColor = .white
        nameLb.font = .systemFont(ofSize: 14)
        return nameLb
    }()
    
}



extension BaseCollectionViewCell
{
    fileprivate func setupUI() {
        
        setupTitleLabels()
    }
    
    fileprivate func setupTitleLabels() {
        
        self.contentView.addSubview(nameLb)
        nameLb.snp.makeConstraints { make in
            make.centerX.centerY.equalTo(self.contentView)
        }
        
    }
}


VC 中的实现

// 皮肤

import UIKit
import SnapKit

private let NormalCellID = "BaseCollectionViewCell"

class AVC: BaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = XColor.x_randomColor
        setUpUI()
        AddNavUI()
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: SW/5, height: SW/5)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        let collection = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collection.register(BaseCollectionViewCell.self, forCellWithReuseIdentifier: NormalCellID)
        collection.dataSource = self
        collection.delegate = self
        collection.backgroundColor = UIColor.white
        return collection
    }()
    
    private lazy var dataArys: NSMutableArray = {
        let dataArys = NSMutableArray()
        return dataArys
    }()
    
}

// MARK: -----
extension AVC
{
    private func setUpUI()
    {
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(TopHeight)
            make.left.right.bottom.equalTo(0)
        }
        
        for i in 0 ..< 100
        {
            let model = BaseModel()
            model.name = "哈哈哈" + " \(i) "
            dataArys.add(model)
        }
        collectionView.reloadData()
    }
    
    func AddNavUI() {
        
        let homeNav = XNavView()
        homeNav.titleName = "皮肤选择"
        homeNav.isBackBtnHid = false
        homeNav.onBackBlock = { isok in
            self.navigationController?.popViewController(animated: true)
        }
        homeNav.navLineColor = .red
        view.addSubview(homeNav)
        homeNav.snp.makeConstraints { make in
            make.left.top.right.equalTo(0)
            make.height.equalTo(TopHeight)
        }
        
    }
    
}


extension AVC: UICollectionViewDelegate, UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dataArys.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = BaseCollectionViewCell.initWithCollectionViewCell(collectionView, indexPath as NSIndexPath, NormalCellID)
        cell.cellForModel(model: (dataArys[indexPath.row] as! BaseModel))
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    }
    
}

还请大家指出问题,改进改进。

柴犬安详.jpg

还请大家指出问题,改进改进。

上一篇 下一篇

猜你喜欢

热点阅读