定义一个collectionview
/// 创建colloectionview
private func createCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.vertical
// 以下两行非常重要 必须设置headerReferenceSize或者footerReferenceSize的大小才能征程的显示header和footer
layout.headerReferenceSize = CGSize(width: kScreenWidth, height: 45)
layout.footerReferenceSize = CGSize(width: kScreenWidth, height: 50)
self.layout = layout
// 布局
layout.itemSize = CGSize(width: kScreenWidth / 5,height: 80)
let width = (kScreenWidth - 3 * 80) / 4
//列间距,行间距,偏移
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 10
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
let rect = CGRect(x: 0, y: 0, width: kScreenWidth, height: CGFloat(kScreenHeight) - CGFloat(kStatusBarHeight) - 44)
collectionView = UICollectionView.init(frame: rect, collectionViewLayout: layout)
collectionView?.delegate = self as UICollectionViewDelegate
collectionView?.dataSource = self as UICollectionViewDataSource;
//注册一个cell
let nib = UINib(nibName: "SCTHomeNormalCollectioncell", bundle: nil)
collectionView?.register(nib, forCellWithReuseIdentifier: "SCTHomeNormalCollectioncell")
// register a footer nib
let nibFooter = UINib(nibName: "SCTCollectionReusebleFooterView", bundle: nil)
// collectionView?.register(nibFooter, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SCTCollectionReusebleFooterView")
// collectionView?.register(SCTCollectionReusableFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SCTCollectionReusableFooterView")
collectionView?.register(nibFooter, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "UICollectionElementKindSectionFooter")
// register a header nib
let nibHeader = UINib(nibName: "SCTCollectionReusableHeaderView", bundle: nil)
// collectionView?.register(nibHeader, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SCTCollectionReusableHeaderView")
// collectionView?.register(nibHeader, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SCTCollectionReusableHeaderView")
collectionView?.register(nibHeader, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UICollectionElementKindSectionHeader")
collectionView?.backgroundColor = UIColor.clear
self.view.addSubview(collectionView!)
}
实现协议
UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
collectionView的协议中实现header和footer
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableview:UICollectionReusableView!
if kind == UICollectionElementKindSectionHeader
{
reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UICollectionElementKindSectionHeader", for: indexPath) as! SCTCollectionReusableHeaderView
if singleShowItemIndex == 0 {
(reusableview as! SCTCollectionReusableHeaderView).tipTitle.text = smartSectionTitles[indexPath.section]
(reusableview as! SCTCollectionReusableHeaderView).colorView.backgroundColor = colorLabels[indexPath.section]
} else {
(reusableview as! SCTCollectionReusableHeaderView).tipTitle.text = smartSectionTitles[singleShowItemIndex - 1]
(reusableview as! SCTCollectionReusableHeaderView).colorView.backgroundColor = colorLabels[singleShowItemIndex - 1]
}
}
else if kind == UICollectionElementKindSectionFooter
{
reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UICollectionElementKindSectionFooter", for: indexPath) as! SCTCollectionReusebleFooterView
} // print("\(kind)--\(indexPath.section)---\(smartSectionTitles.count - 1)")
return reusableview
}
具体定义每个footer或者header的size
举例为footer
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
if section == smartSectionTitles.count - 1 {
return CGSize(width: kScreenWidth, height: 60)
} else {
return CGSize(width: kScreenWidth, height: 0)
}
}
时间不够,只能这么帖代码了,以后有时间了来完善这些内容,抱歉