Swift之collectionView的基本使用
2017-04-03 本文已影响4730人
张不二01
collectionView.png
1, 首先创建collectionView,并设置数据源和代理
self.collectionView = UICollectionView.init(frame: self.view.bounds)
self.view.addSubview(self.collectionView)
self.collectionView.dataSource = self
self.collectionView.delegate = self
2,设置collectionView的布局(可自行进行封装)
let layout = UICollectionViewFlowLayout.init()
layout.itemSize = CGSize.init(width: 100, height: 100)
layout.minimumLineSpacing = 30
self.minimumInteritemSpacing = 50
layout.sectionInset = UIEdgeInsets.init(top: 0, left: 30, bottom: 0, right: 30)
self.collectionView.collectionViewLayout = layout
3,注册cell,如果需要还可以注册header和footer(我这里是通过nib进行注册,也可以直接通过代码进行注册)
//cell注册
self.collectionView.register(UINib.init(nibName: "IMCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
//header注册
self.collectionView.register(UINib.init(nibName: "IMCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "UICollectionElementKindSectionHeader", withReuseIdentifier: "header")
//footer注册
self.collectionView.register(UINib.init(nibName: "IMCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "UICollectionElementKindSectionFooter", withReuseIdentifier: "footer")
4,遵守相应的数据源和代理,实现相应的方法
//MARK:- collectionView的数据源和代理方法
extension IMCollectionViewViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
//cellForItemAt indexPath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
//这个是设定header和footer的方法,根据kind不同进行不同的判断即可
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == "UICollectionElementKindSectionHeader"{
let resuableView = collectionView.dequeueReusableSupplementaryView(ofKind: "UICollectionElementKindSectionHeader", withReuseIdentifier: "header", for: indexPath) as! IMCollectionReusableView
resuableView.resuableKindLabel.text = "header"
resuableView.backgroundColor = UIColor.red
return resuableView
}else{
let resuableView = collectionView.dequeueReusableSupplementaryView(ofKind: "UICollectionElementKindSectionFooter", withReuseIdentifier: "footer", for: indexPath) as! IMCollectionReusableView
resuableView.resuableKindLabel.text = "footer"
resuableView.backgroundColor = UIColor.blue
return resuableView
}
}
//header高度
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize.init(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height*0.1)
}
//footer高度
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize.init(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height*0.1)
}
}