9.1、UICollectionView自定义Header、系统Cell、自定义Cell

1、NewCollectionView

import UIKit
class NewCollectionView: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    let ScreenWidth = UIScreen.main.bounds.width
    let ScreenHeight = UIScreen.main.bounds.height
    func createCollectionView() {
        let flayout = UICollectionViewFlowLayout()
        //设置item尺寸
        flayout.itemSize = CGSize(width: (ScreenWidth-20)/3, height: 170)
        //设置左右间隔
//        flayout.minimumInteritemSpacing = 10
        //设置上下间隔 最小行间距
//        flayout.minimumLineSpacing = 10
        //设置滚动方向
        flayout.scrollDirection = .vertical
        //初始化
        let collecView = UICollectionView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), collectionViewLayout: flayout)
        self.addSubview(collecView)
        collecView.delegate = self
        collecView.dataSource = self
        collecView.backgroundColor = UIColor.gray
        collecView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "systemCell")
        //自定义cell
        //        collecView.register(NewCell.self, forCellWithReuseIdentifier: "newCell")
        collecView.register(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HEAD_ID");

        collecView.alwaysBounceVertical = true;
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let systemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "systemCell", for: indexPath)
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: (ScreenWidth-20)/3, height: 50))
        systemCell.contentView.addSubview(label)
        label.text = "热门活动"
        let imageView = UIImageView(frame: CGRect(x: 0, y: 60, width: (ScreenWidth-20)/3, height: 110))
        systemCell.contentView.addSubview(imageView)
        imageView.image = UIImage(named: "IMG_0492")
        systemCell.backgroundColor = UIColor.red
//        自定义cell
        //        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newCell", for: indexPath) as! NewCell
        return systemCell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: ScreenWidth, height: 40)
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var headrView = HeaderCollectionReusableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
        if kind == UICollectionElementKindSectionHeader {
            headrView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HEAD_ID", for: indexPath) as! HeaderCollectionReusableView
        }
        return headrView
    }
//        除了定义FlowLayout外的另外一种设定item的方法:
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        return CGSize(width: (ScreenWidth-20)/3, height: 170)
//    }
}

2、自定义组头

class HeaderCollectionReusableView: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        let label = UILabel(frame: CGRect(x: 10, y: 0, width: 120, height: 40))
        self.addSubview(label)
        label.text = "热门活动"
        let imageView = UIImageView(frame: CGRect(x: 250, y: 0, width: 150, height: 40))
        self.addSubview(imageView)
        imageView.image = UIImage(named: "sy-xfd-tag")
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

3、代码自定义Cell

import UIKit
class NewCell: UICollectionViewCell {
    let ScreenWidth = UIScreen.main.bounds.width
    let ScreenHeight = UIScreen.main.bounds.height
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.createCollecViewCell()
    }
//    当对象需要保存自身时-encoderWithCoder:方法被调用
//    当对象需要加载自身时-initWithCoder:方法被调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func createCollecViewCell() {
        let toplabel = UILabel(frame: CGRect(x: 0, y: 0, width: (ScreenWidth-20)/3, height: 30))
        self.addSubview(toplabel)
        toplabel.text = "顶部的文字"
        toplabel.textColor = UIColor.black
        toplabel.font = UIFont.systemFont(ofSize: 18)
        let mlabel = UILabel(frame: CGRect(x: 0, y: 30, width: (ScreenWidth-20)/3, height: 30))
        self.addSubview(mlabel)
        mlabel.text = "中间的图是招财猫"
        mlabel.textColor = UIColor.gray
        mlabel.font = UIFont.systemFont(ofSize: 15)

        let bottomImage = UIImageView(frame: CGRect(x: 0, y: 60, width: (ScreenWidth-20)/3, height: 110))
        self.addSubview(bottomImage)
        
        bottomImage.image = UIImage.init(named: "IMG_0491")
    }
}

你可能感兴趣的:(9.1、UICollectionView自定义Header、系统Cell、自定义Cell)