// // testViewController.swift // 06-UICollectionView // // Created by CJ Wang on 2020/3/5. // Copyright © 2020. All rights reserved. //

import UIKit

class TestViewController: UIViewController {

let SCREEN_W = UIScreen.main.bounds.size.width
let SCREEN_H = UIScreen.main.bounds.size.height

let layout = UICollectionViewLayout()
var collectionView: UICollectionView?
let cellHeightArry: NSMutableArray = []

private lazy var headerTopView: UIView  = {
    
    let headV = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 300))
    headV.backgroundColor = .green
  
    let imageView = UIImageView()
    imageView.frame = headV.frame
    imageView.image = UIImage(named: "cutest.jpg")
   
    headV.addSubview(imageView)
    
    return headV
}()



override func viewDidLoad() {
    super.viewDidLoad()
    
    setupUI()

    // Do any additional setup after loading the view.
}


private func setupUI(){
    
    //初始化layout
    let layout = UICollectionViewFlowLayout()
    
    //layout的attributes 既可以这样直接设置,也可以通过代理来设置效果是一样的
    
    let itemW = (SCREEN_W - 30)/2
    layout.itemSize = CGSize(width: itemW, height: itemW + 40)
    
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    layout.minimumLineSpacing = 5
    layout.minimumInteritemSpacing = 10
    
    //layout.scrollDirection = .horizontal 横向滑动
    layout.scrollDirection = .vertical // 竖向滑动
    
    
    collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: SCREEN_H),collectionViewLayout: layout)
    collectionView?.backgroundColor = .white
    
    collectionView?.register(TestCollectionViewCell.self, forCellWithReuseIdentifier: "CCCCC")

    collectionView?.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerIdentifier")
    collectionView?.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footerIdentfier")
  

    collectionView?.dataSource = self
    collectionView?.delegate = self
    
    view.addSubview(collectionView!)
    
}


}


extension TestViewController:UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

//MARK:UICollectionViewDataSource
   func numberOfSections(in collectionView: UICollectionView) -> Int {
       return 1
   }
   
   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return 20
   }
   
   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCCCC", for: indexPath) as! TestCollectionViewCell
    
    cell.layer.cornerRadius = 5
    cell.layer.masksToBounds = true
    
    return cell
    

   }


//MARK: header footer

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    
    if kind == UICollectionView.elementKindSectionHeader{
        
        let headerV = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerIdentifier", for: indexPath)
        
        headerV.addSubview(headerTopView)
        
        headerV.backgroundColor = .red
        
        return headerV
        
    }else if kind == UICollectionView.elementKindSectionFooter{
        
        let footer = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footerIdentfier", for: indexPath)
        footer.backgroundColor = .blue
        
        return footer
        
    }
    
    return UICollectionReusableView.init()

    
}


//MARK: -headerView Height

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    
    return CGSize(width: SCREEN_W, height: 300)
    
}

//MARK: - footerView Height

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    
    return CGSize(width: SCREEN_W, height: 700)
    
}


//MARK: - item size

 //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//
//        let itemW = (SCREEN_W - 50)/3
//
//        return CGSize(width: itemW, height: itemW + 200)
//
//    }

//MARK: - padings

//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//
//        return UIEdgeInsets(top: 10, left: 10,   bottom: 10, right: 10)
//    }


//MARK: - hang mini ling

//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
 //
 //
 //        return 15
//    }

//MARK - lie mini ling

//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
//        return 5
//    }


//MARK  - item actions

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    
    print("888888888888888899999999")
}


//MARK - item heightlight

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    
    let  cell = collectionView.cellForItem(at: indexPath)
    cell?.backgroundColor = .white
    
}

//MARK: - item cancel heightLight

func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
    
    let cell  = collectionView.cellForItem(at: indexPath)
    cell?.backgroundColor = UIColor(red: 200/255.0, green: 100/255.0, blue: 50/255.0, alpha: 1)
    
    let cv = TestRandomViewController()
    self.navigationController?.pushViewController(cv, animated: true)


}


}
Copy the code