swift UICollectionView使用和数据加载

719 阅读1分钟

初始化UICollectionView

class FF_VCText:UIViewController{
    private var vv_collectionView:UICollectionView!
    public let vv_kScreenWidth = UIScreen.main.bounds.width
    private var vv_arrData = [DomeChildResourceBean]()//数据源
    override func viewDidLoad() {
        super.viewDidLoad()
        vv_layout = UICollectionViewFlowLayout()  //定义Cell
        vv_layout.scrollDirection* = .vertical    //vertical垂直滑动 horizontal 横向滑动
        vv_layout.minimumLineSpacing = 10  //行间距
        vv_layout.minimumInteritemSpacing = 10  //每个cell间的横向间距
        vv_layout.itemSize = CGSize(width: 120, height: 120) //cell大小
        vv_layout.headerReferenceSize = CGSize.init(width: vv_kScreenWidth, height: 80//HeaderView的大小
        vv_layout.footerReferenceSize = CGSize.init(width: vv_kScreenWidth, height: 60//FooterView的大小
        vv_collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: vv_layout)
        vv_collectionView.register(FF_CVCellRefresh.self, forCellWithReuseIdentifier: "FF_CVCellRefresh")
        vv_collectionView.register(FF_CVCellHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "FF_CVCellHeader")   //添加HeaderView
        vv_collectionView.register(FF_CVCellFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "FF_CVCellFooter")    //添加FooterView
        vv_collectionView.showsVerticalScrollIndicator = false
        vv_collectionView.backgroundColor = .clear
        vv_collectionView.allowsMultipleSelection = true
        vv_collectionView.delegate = self
        vv_collectionView.dataSource = self
        vv_collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        self.view.addSubview(vv_collectionView)
        vv_collectionView.snp.makeConstraints{ (m) in  //我这里使用的是SnapKit布局
            m.edges.equalToSuperview()
        }
        for index in (1)..<(4){
        var vv_arr = [DemeResourceBean]()
        vv_arr.append(DemeResourceBean(content: "内容\(index)-1"))
        vv_arr.append(DemeResourceBean(content: "内容\(index)-2"))
        vv_arr.append(DemeResourceBean(content: "内容\(index)-3"))
        vv_arr.append(DemeResourceBean(content: "内容\(index)-4"))
        self.vv_arrData.append(DomeChildResourceBean(header: "标题\(index)",footer: "底部View\(index)", resource: vv_arr))
        }
        self.vv_collectionView.reloadData()
    }
    

实现代理

extension FF_VCText: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return vv_arrData.count
    }
  
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return vv_arrData[section].resource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let vv_cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FF_CVCellRefresh", for: indexPath) **as**! FF_CVCellRefresh
        let vv_model = vv_arrData[indexPath.section].resource[indexPath.row]
        vv_cell.backgroundColor = UIColor.init(ff_rgb: 0x999999)
        vv_cell.vv_title.text = vv_model.content
        return vv_cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let vv_model = vv_arrData[indexPath.section]
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let vv_cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FF_CVCellHeader", for: indexPath) **as**! FF_CVCellHeader
            vv_cell.vv_title.backgroundColor = .red
            vv_cell.vv_title.setTitle(vv_model.header, for: .normal)

            return vv_cell
        case UICollectionView.elementKindSectionFooter:
            let vv_cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FF_CVCellFooter", for: indexPath) **as**! FF_CVCellFooter
            vv_cell.backgroundColor = .blue
            vv_cell.vv_title.setTitle(vv_model.footer, for: .normal)
            return vv_cell
        default:
            fatalError("No such kind")
        }
    }
    
    //设置不同cell1的size
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        switch indexPath.section {
//        case 0:
//            return CGSize(width: 150, height: 150)
//        default:
//            return CGSize(width: 120, height: 120)
//        }
//    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(ff_tip: vv_arrData[indexPath.section].resource[indexPath.row].content)
    }
}

class FF_CVCellRefresh: UICollectionViewCell {
    lazy var vv_title:UILabel = {
        let vv = UILabel.ff_label(ff_text: "", ff_color: .white, ff_align: .center, ff_font: .ff_PFSCFont(ff_size: 16*vv_kWidthRatio))
        addSubview(vv)
        vv.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return vv
    }()
}

class FF_CVCellHeader: UICollectionReusableView {
    lazy var vv_title:UIButton = {
        let vv = UIButton()
        addSubview(vv)
        vv.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.right.bottom.equalToSuperview()
        }
        return vv
    }()
}

class FF_CVCellFooter: UICollectionReusableView {
    lazy var vv_title:UIButton = {
        let vv = UIButton()
        addSubview(vv)
        vv.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return vv
    }()
}

struct DomeChildResourceBean:Codable {
    let header:String
    let footer:String
    var resource:[DemeResourceBean]
}

struct DemeResourceBean:Codable {
    let content:String
}