import UIKit
class ThirdViewController: UIViewController{
struct Key {
struct Identity {//cell相关的注册
static let headerIdentity = "header"
static let footerIdentity = "footer"
static let cellIdentity = "cell"
}
struct cellItemSize {//每组cell items的size.width
static var cellFirstWidth = KscreenWidth
static let cellSecondWidth = (KscreenWidth - 5*2 - 3*4)/5
static let cellThirdWidth = (KscreenWidth - 5*2 - 3*6)/7
}
}
var collecV:UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "三"
// let layout = TestUICollectionViewFlowLayout.init()
let layout = UICollectionViewFlowLayout.init()
// layout.itemSize = CGSize(width: 60, height: 60)
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 2
layout.scrollDirection = .vertical
layout.sectionInset = UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5)
layout.headerReferenceSize = CGSize(width: KscreenWidth, height: 80)
layout.footerReferenceSize = CGSize(width: KscreenWidth, height: 80)
self.collecV = UICollectionView.init(frame: CGRect.null, collectionViewLayout: layout)
self.collecV.backgroundColor = UIColor.white
//注册
self.collecV.register(CollectionViewCellTest.self, forCellWithReuseIdentifier: Key.Identity.cellIdentity)
collecV.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Key.Identity.headerIdentity);
collecV.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: Key.Identity.footerIdentity)
self.collecV.delegate = self
self.collecV.dataSource = self
self.view.addSubview(self.collecV)
self.collecV.snp.makeConstraints { (make) in
make.left.right.top.bottom.equalToSuperview()
}
}
}
extension ThirdViewController:UICollectionViewDelegateFlowLayout{
//MARK: - UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//每组item大小
if indexPath.section == 0 {
return CGSize(width: Key.cellItemSize.cellFirstWidth, height: 100)
}
if indexPath.section == 1 {
return CGSize(width: Key.cellItemSize.cellSecondWidth, height: 50)
}
if indexPath.section == 2 {
return CGSize(width:Key.cellItemSize.cellThirdWidth, height: 30)
}
return CGSize(width: 0, height: 0)
}
//
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
//
// //行间距
// }
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
//
// //item之间的距离
// }
//
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//
// //每组之间的距离
// }
//
//
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
// //头试图 大小
// }
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
// //尾部试图大小
// }
}
extension ThirdViewController:UICollectionViewDelegate,UICollectionViewDataSource{
//MARK: - UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return 10
}
if section == 1 {
return 13
}
if section == 2 {
return 8
}
return 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:CollectionViewCellTest = collectionView.dequeueReusableCell(withReuseIdentifier: Key.Identity.cellIdentity, for: indexPath) as! CollectionViewCellTest
cell.backgroundColor = UIColor.brown
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader{
let headerView : UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Key.Identity.headerIdentity, for: indexPath)
headerView.backgroundColor = UIColor.yellow
return headerView
}else{
let footView : UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: Key.Identity.footerIdentity, for: indexPath)
footView.backgroundColor = UIColor.purple
return footView
}
}
}
二 .cell
import UIKit
class CollectionViewCellTest: UICollectionViewCell {
var lab:UILabel!
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
lab = UILabel.init()
lab.text = "hello world!"
lab.numberOfLines = 0
lab.font = UIFont.systemFont(ofSize: 14)
self.contentView.addSubview(lab)
lab.snp.makeConstraints { (make) in
//make.size.equalTo(CGSize(width: self.frame.width, height: 30))
// make.width.equalTo(self.frame.width)
// make.center.equalToSuperview()
make.left.equalToSuperview()
make.width.equalToSuperview()
make.top.equalToSuperview().offset(5)
}
}
}