Description:
UICollectionView protocol是繼承自UIScrollView protocol,可以用來實現瀑布流、網格、或是iOS內建相簿app的照片排列方式。
Component:
1.UICollectionView
Highlight function:
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
layout.minimumLineSpacing = 10 // space between each cell row
layout.minimumInteritemSpacing = 10 // space between each cell
layout.scrollDirection = .horizontal // scroll direction
layout.itemSize = CGSize(width: CGFloat(fullSize.width) / 3 - 10,
height: CGFloat(fullSize.height) / 3 - 10)
layout.headerReferenceSize = CGSize(width: fullSize.width, height: 40) // header zone
layout.footerReferenceSize = CGSize(width: fullSize.width, height: 40) // footer zone
let myCollectionView = UICollectionView(frame: CGRect(x: 0,
y: 20,
width: fullSize.width,
height: fullSize.height - 20),
collectionViewLayout: layout)
myCollectionView.register(CollectionViewCell.self,
forCellWithReuseIdentifier: "Cell")
myCollectionView.register(UICollectionReusableView.self,
forSupplementaryViewOfKind:
UICollectionElementKindSectionHeader,
withReuseIdentifier: "Header")
myCollectionView.register(UICollectionReusableView.self,
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter,
withReuseIdentifier: "Footer")
3.透過UICollectionViewCell protocol來自定所需之cell layout:
class CollectionViewCell: UICollectionViewCell {
var imgView: UIImageView!
var titleLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let width = Double(UIScreen.main.bounds.size.width)
// UIImageView
imgView = UIImageView(frame: CGRect(x: 0,
y: 0,
width: width/3 - 10.0,
height: width/3 - 10.0))
self.addSubview(imgView)
// UILabel
titleLabel = UILabel(frame: CGRect(x: 0,
y: 0,
width: Int(width / 3 - 10),
height: 40))
titleLabel.textAlignment = .center
titleLabel.textColor = UIColor.orange
self.addSubview(titleLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
4.使用UICollectionViewDataSource protocol來完成畫面結果:
// number of cells in a section
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
// reused cell with identifier
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.imgView.image = UIImage(named: "0\(indexPath.item + 1).jpg")
cell.titleLabel.text = "0\(indexPath.item + 1)"
return cell
}
// number of section
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
Additional:
若是要顯示header或footer,可透過透過UICollectionViewDataSource protocol中的viewForSupplementaryElementOfKind()來顯示,此function提供了"kind"這個參數來辨別header或是footer:
// set reuse header and footer in section
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//Create UICollectionReusableView
var reusableView = UICollectionReusableView()
let label = UILabel(frame: CGRect(x: 0,
y: 0,
width: fullSize.width,
height: 40))
label.textAlignment = .center
if kind == UICollectionElementKindSectionHeader {
// header
reusableView = collectionView.dequeueReusableSupplementaryView(
ofKind:
UICollectionElementKindSectionHeader,
withReuseIdentifier: "Header",
for: indexPath)
//header content
reusableView.backgroundColor = UIColor.darkGray
label.text = "Header";
label.textColor = UIColor.white
} else if kind == UICollectionElementKindSectionFooter {
// footer
reusableView = collectionView.dequeueReusableSupplementaryView(
ofKind:
UICollectionElementKindSectionFooter,
withReuseIdentifier: "Footer",
for: indexPath)
//footer content
reusableView.backgroundColor = UIColor.cyan
label.text = "Footer";
label.textColor = UIColor.black
}
reusableView.addSubview(label)
return reusableView
}
Reference:
Source code on Github