Swift-UICollectionView快捷创建(刨坟)

UICollectionView

懒加载

   private lazy var collectionView: UICollectionView = {
        //每行数量
        let rowCount = 4
        //行间距
        let space: CGFloat = 20
        //列间距
        let margin: CGFloat = 20
        let itemWidth = (kScreenWidth - margin * 2 - CGFloat(rowCount - 1) * margin) / CGFloat(rowCount)
        let itemHight: CGFloat = itemWidth + 20
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: itemWidth, height: itemHight)
        layout.minimumLineSpacing = space
        layout.minimumInteritemSpacing = margin
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: space, left: margin, bottom: space, right: margin)
        // 设置分区头视图和尾视图宽高
        //        layout.headerReferenceSize = CGSize(width: kScreenWidth, height: 80)
        //        layout.footerReferenceSize = CGSize(width: kScreenWidth, height: 80)
        let collectView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), collectionViewLayout: layout)
        // 注册cell
        collectView.register(FQScanOrderFoodGoodsCell.self, forCellWithReuseIdentifier: "FQScanOrderFoodGoodsCell")
        collectView.delegate = self
        collectView.dataSource = self
        collectView.backgroundColor = .white
        return collectView
    }()
extension FQScanOrderFood_RewardVC: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return   0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FQScanOrderFoodGoodsCell", for: indexPath) as! FQScanOrderFoodGoodsCell

        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }

}

UICollectionViewCell

class FQScanOrderFoodGoodsCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
 
    func setupSubViews() {
    
}

你可能感兴趣的:(IOS---从0出发)