懒加载实现
lazy var collectView:UICollectionView = {
let layout = UICollectionViewFlowLayout.init()
layout.itemSize = CGSize(width: kScreen_width / 2 , height: kScreen_width / 2)
layout.minimumLineSpacing=1;
layout.minimumInteritemSpacing=1
layout.footerReferenceSize = CGSize(width: screenWidth, height: 50)
layout.headerReferenceSize = CGSize(width: screenWidth, height: 50)
let collectView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
collectView.backgroundColor = .groupTableViewBackground
collectView.delegate = self
collectView.dataSource = self
collectView.showsVerticalScrollIndicator = true
collectView.register(SwiftFooterCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "SwiftFooterCollectionReusableView")
collectView.register(SwiftCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "SwiftCollectionViewCell") collectView.register(SwiftHeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SwiftHeaderCollectionReusableView")
return collectView
}()
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellString = "SwiftCollectionViewCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellString, for: indexPath)
cell.backgroundColor = UIColor.gray
return cell
}
//设置 区头和区尾
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let headerView:SwiftHeaderCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SwiftHeaderCollectionReusableView", for: indexPath) as! SwiftHeaderCollectionReusableView
return headerView
}
else
{
let footerView:SwiftFooterCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SwiftFooterCollectionReusableView", for: indexPath) as! SwiftFooterCollectionReusableView
return footerView
}
}
网友评论