这次Demo是关于UICollectionView的
//
// ViewController.swift
// UIKitPrograming
//
import UIKit
class ViewController: UIViewController {
// 定义一些不变量
let ScreenWidth = UIScreen.main.bounds.width // 屏幕宽度的一半
let cellIdentifier = "cell"
let headerIdentifier = "header"
let footerIdentifier = "footer"
let headerKind = UICollectionView.elementKindSectionHeader
let footerKind = UICollectionView.elementKindSectionFooter
// 返回一个随机的颜色
func randomColor() -> UIColor {
return UIColor(displayP3Red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 先设置布局,这是collectionView必须做的
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 5
layout.itemSize = CGSize(width: ScreenWidth / 7 - layout.minimumInteritemSpacing * 2, height: ScreenWidth / 7 - layout.minimumInteritemSpacing * 2) // 设置方块的长和宽
layout.scrollDirection = .vertical // 垂直滚动
layout.sectionInset = .init(top: 5, left: 5, bottom: 5, right: 5) // 设置每个方块的四周空隙
layout.headerReferenceSize = CGSize(width: ScreenWidth, height: 80) // 设置header大小
layout.footerReferenceSize = CGSize(width: ScreenWidth, height: 80)
// 第二步是创建一个UICollectionView的实例
let colletionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
colletionView.backgroundColor = UIColor.white
// 设置代理
colletionView.delegate = self
colletionView.dataSource = self
// 注册方块
colletionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
colletionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: headerKind, withReuseIdentifier: headerIdentifier)
colletionView.register(CollectionFooterView.self, forSupplementaryViewOfKind: footerKind, withReuseIdentifier: footerIdentifier)
// 别忘了最后要添加实例
view.addSubview(colletionView)
}
}
// 下面要实现委托的内容
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// 一个section有多少个方块
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 40
}
// 几个section
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
// 返回cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// cell一定要用dequeueReusableCell的方法去取
let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
cell.backgroundColor = randomColor() // 设置cell的颜色随机
return cell
}
// 点击方块,打印他的编号
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row)
}
// 这个函数是针对header和footer来做的,返回值是UICollectionReusableView类型,这个类型继承自UIView,因此可以理解成一个UIView
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == headerKind {
// CollectionHeaderView就是UICollectionReusableView类型的
let headView: CollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: headerKind, withReuseIdentifier: headerIdentifier, for: indexPath) as! CollectionHeaderView
headView.title.text = "This is HeaderView"
headView.backgroundColor = UIColor.gray
headView.setUpView()
return headView
} else {
let footerView: CollectionFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: footerKind, withReuseIdentifier: footerIdentifier, for: indexPath) as! CollectionFooterView
footerView.title.text = "This is FooterView"
footerView.backgroundColor = UIColor.gray
footerView.setUpView()
return footerView
}
}
}
//
// CollectionHeaderView.swift
// UIKitPrograming
//
import UIKit
import SnapKit
class CollectionHeaderView: UICollectionReusableView {
lazy var title: UILabel = {
let title: UILabel = UILabel()
title.font = UIFont.boldSystemFont(ofSize: 14)
title.textColor = UIColor.black
title.textAlignment = .center
return title
}()
func setUpView() {
addSubview(title)
title.snp.makeConstraints { (make) in
make.center.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.5)
make.height.equalToSuperview().multipliedBy(0.5)
}
}
}
//
// CollectionFooterView.swift
// UIKitPrograming
//
import UIKit
class CollectionFooterView: UICollectionReusableView {
lazy var title: UILabel = {
let title: UILabel = UILabel()
title.font = UIFont.boldSystemFont(ofSize: 14)
title.textColor = UIColor.black
title.textAlignment = .center
return title
}()
func setUpView() {
addSubview(title)
title.snp.makeConstraints { (make) in
make.center.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.5)
make.height.equalToSuperview().multipliedBy(0.5)
}
}
}
|
请发表评论