初始化
lazy var clvData: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
layout.minimumInteritemSpacing = 0; //列與列之間的間距
layout.minimumLineSpacing = 0;//行與行之間的間距
layout.itemSize = CGSize.init(width: kSCREEN_WIDTH/3, height: 100)
let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.white
collectionView.delegate = self
collectionView.dataSource = self
let cell = UINib.init(nibName: "KSampleClvCell", bundle: nil)
collectionView.register(cell, forCellWithReuseIdentifier: "KSampleClvCell")
return collectionView
}()
self.view.addSubview(clvData)
clvData.snp.makeConstraints { (make) -> Void in
make.edges.equalTo(self.view)
}
常用代理方法
extension WorkHomeVC: UICollectionViewDelegate, UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KSampleClvCell", for: indexPath) as! KSampleClvCell
return cell
}
}
注冊(cè)cell,SectionHead,SectionFoot
#IB 類(lèi)型
let secionHead = UINib.init(nibName: "MMSectionHeadView", bundle: nil)
collectionView.register(secionHead, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MMSectionHeadView")
collectionView.register(secionHead, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "MMSectionHeadView")
#class 類(lèi)型
collectionView.register(MMCollectCell.self, forCellWithReuseIdentifier:"MMCollectCell")
collectionView.register(MMSectionHeadView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "MMSectionHeadView")
collectionView.register(MMSectionFootView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionFooter, withReuseIdentifier: "MMSectionFootView")
自定義 分區(qū)頭部显拜,尾部代理
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
{
var v = UICollectionReusableView()
if kind == UICollectionElementKindSectionHeader
{
v = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MMSectionHeadView", for: indexPath)as! MMSectionHeadView
}
else if kind == UICollectionElementKindSectionFooter
{
v = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "MMSectionFootView", for: indexPath)as! MMSectionFootView
}
return v
}
/* sectionHeadView 尺寸*/
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize(width: UIScreen.main.bounds.width, height: 50)
}
/* sectionFootView 尺寸*/
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
return CGSize(width: UIScreen.main.bounds.width, height: 50)
}
item Size 代理
/* item 尺寸*/
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: UIScreen.main.bounds.width/2, height: 100)
}
collectionView 方法特性
##水平滑動(dòng)
layout.scrollDirection = .horizontal
錯(cuò)誤