初始化UICollectionView
class FF_VCText:UIViewController{
private var vv_collectionView:UICollectionView!
public let vv_kScreenWidth = UIScreen.main.bounds.width
private var vv_arrData = [DomeChildResourceBean]()
override func viewDidLoad() {
super.viewDidLoad()
vv_layout = UICollectionViewFlowLayout()
vv_layout.scrollDirection* = .vertical
vv_layout.minimumLineSpacing = 10
vv_layout.minimumInteritemSpacing = 10
vv_layout.itemSize = CGSize(width: 120, height: 120)
vv_layout.headerReferenceSize = CGSize.init(width: vv_kScreenWidth, height: 80)
vv_layout.footerReferenceSize = CGSize.init(width: vv_kScreenWidth, height: 60)
vv_collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: vv_layout)
vv_collectionView.register(FF_CVCellRefresh.self, forCellWithReuseIdentifier: "FF_CVCellRefresh")
vv_collectionView.register(FF_CVCellHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "FF_CVCellHeader")
vv_collectionView.register(FF_CVCellFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "FF_CVCellFooter")
vv_collectionView.showsVerticalScrollIndicator = false
vv_collectionView.backgroundColor = .clear
vv_collectionView.allowsMultipleSelection = true
vv_collectionView.delegate = self
vv_collectionView.dataSource = self
vv_collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
self.view.addSubview(vv_collectionView)
vv_collectionView.snp.makeConstraints{ (m) in
m.edges.equalToSuperview()
}
for index in (1)..<(4){
var vv_arr = [DemeResourceBean]()
vv_arr.append(DemeResourceBean(content: "内容\(index)-1"))
vv_arr.append(DemeResourceBean(content: "内容\(index)-2"))
vv_arr.append(DemeResourceBean(content: "内容\(index)-3"))
vv_arr.append(DemeResourceBean(content: "内容\(index)-4"))
self.vv_arrData.append(DomeChildResourceBean(header: "标题\(index)",footer: "底部View\(index)", resource: vv_arr))
}
self.vv_collectionView.reloadData()
}
实现代理
extension FF_VCText: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return vv_arrData.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return vv_arrData[section].resource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let vv_cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FF_CVCellRefresh", for: indexPath) **as**! FF_CVCellRefresh
let vv_model = vv_arrData[indexPath.section].resource[indexPath.row]
vv_cell.backgroundColor = UIColor.init(ff_rgb: 0x999999)
vv_cell.vv_title.text = vv_model.content
return vv_cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let vv_model = vv_arrData[indexPath.section]
switch kind {
case UICollectionView.elementKindSectionHeader:
let vv_cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FF_CVCellHeader", for: indexPath) **as**! FF_CVCellHeader
vv_cell.vv_title.backgroundColor = .red
vv_cell.vv_title.setTitle(vv_model.header, for: .normal)
return vv_cell
case UICollectionView.elementKindSectionFooter:
let vv_cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FF_CVCellFooter", for: indexPath) **as**! FF_CVCellFooter
vv_cell.backgroundColor = .blue
vv_cell.vv_title.setTitle(vv_model.footer, for: .normal)
return vv_cell
default:
fatalError("No such kind")
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(ff_tip: vv_arrData[indexPath.section].resource[indexPath.row].content)
}
}
class FF_CVCellRefresh: UICollectionViewCell {
lazy var vv_title:UILabel = {
let vv = UILabel.ff_label(ff_text: "", ff_color: .white, ff_align: .center, ff_font: .ff_PFSCFont(ff_size: 16*vv_kWidthRatio))
addSubview(vv)
vv.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
return vv
}()
}
class FF_CVCellHeader: UICollectionReusableView {
lazy var vv_title:UIButton = {
let vv = UIButton()
addSubview(vv)
vv.snp.makeConstraints { make in
make.top.equalTo(20)
make.left.right.bottom.equalToSuperview()
}
return vv
}()
}
class FF_CVCellFooter: UICollectionReusableView {
lazy var vv_title:UIButton = {
let vv = UIButton()
addSubview(vv)
vv.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
return vv
}()
}
struct DomeChildResourceBean:Codable {
let header:String
let footer:String
var resource:[DemeResourceBean]
}
struct DemeResourceBean:Codable {
let content:String
}